Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | 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 "program/hash_table.h"
  58. #include "ir.h"
  59.  
  60. static void
  61. detect_conflicting_assignments(struct _mesa_glsl_parse_state *state,
  62.                                exec_list *instructions);
  63.  
  64. void
  65. _mesa_ast_to_hir(exec_list *instructions, struct _mesa_glsl_parse_state *state)
  66. {
  67.    _mesa_glsl_initialize_variables(instructions, state);
  68.  
  69.    state->symbols->separate_function_namespace = state->language_version == 110;
  70.  
  71.    state->current_function = NULL;
  72.  
  73.    state->toplevel_ir = instructions;
  74.  
  75.    /* Section 4.2 of the GLSL 1.20 specification states:
  76.     * "The built-in functions are scoped in a scope outside the global scope
  77.     *  users declare global variables in.  That is, a shader's global scope,
  78.     *  available for user-defined functions and global variables, is nested
  79.     *  inside the scope containing the built-in functions."
  80.     *
  81.     * Since built-in functions like ftransform() access built-in variables,
  82.     * it follows that those must be in the outer scope as well.
  83.     *
  84.     * We push scope here to create this nesting effect...but don't pop.
  85.     * This way, a shader's globals are still in the symbol table for use
  86.     * by the linker.
  87.     */
  88.    state->symbols->push_scope();
  89.  
  90.    foreach_list_typed (ast_node, ast, link, & state->translation_unit)
  91.       ast->hir(instructions, state);
  92.  
  93.    detect_recursion_unlinked(state, instructions);
  94.    detect_conflicting_assignments(state, instructions);
  95.  
  96.    state->toplevel_ir = NULL;
  97.  
  98.    /* Move all of the variable declarations to the front of the IR list, and
  99.     * reverse the order.  This has the (intended!) side effect that vertex
  100.     * shader inputs and fragment shader outputs will appear in the IR in the
  101.     * same order that they appeared in the shader code.  This results in the
  102.     * locations being assigned in the declared order.  Many (arguably buggy)
  103.     * applications depend on this behavior, and it matches what nearly all
  104.     * other drivers do.
  105.     */
  106.    foreach_list_safe(node, instructions) {
  107.       ir_variable *const var = ((ir_instruction *) node)->as_variable();
  108.  
  109.       if (var == NULL)
  110.          continue;
  111.  
  112.       var->remove();
  113.       instructions->push_head(var);
  114.    }
  115. }
  116.  
  117.  
  118. /**
  119.  * If a conversion is available, convert one operand to a different type
  120.  *
  121.  * The \c from \c ir_rvalue is converted "in place".
  122.  *
  123.  * \param to     Type that the operand it to be converted to
  124.  * \param from   Operand that is being converted
  125.  * \param state  GLSL compiler state
  126.  *
  127.  * \return
  128.  * If a conversion is possible (or unnecessary), \c true is returned.
  129.  * Otherwise \c false is returned.
  130.  */
  131. bool
  132. apply_implicit_conversion(const glsl_type *to, ir_rvalue * &from,
  133.                           struct _mesa_glsl_parse_state *state)
  134. {
  135.    void *ctx = state;
  136.    if (to->base_type == from->type->base_type)
  137.       return true;
  138.  
  139.    /* This conversion was added in GLSL 1.20.  If the compilation mode is
  140.     * GLSL 1.10, the conversion is skipped.
  141.     */
  142.    if (!state->is_version(120, 0))
  143.       return false;
  144.  
  145.    /* From page 27 (page 33 of the PDF) of the GLSL 1.50 spec:
  146.     *
  147.     *    "There are no implicit array or structure conversions. For
  148.     *    example, an array of int cannot be implicitly converted to an
  149.     *    array of float. There are no implicit conversions between
  150.     *    signed and unsigned integers."
  151.     */
  152.    /* FINISHME: The above comment is partially a lie.  There is int/uint
  153.     * FINISHME: conversion for immediate constants.
  154.     */
  155.    if (!to->is_float() || !from->type->is_numeric())
  156.       return false;
  157.  
  158.    /* Convert to a floating point type with the same number of components
  159.     * as the original type - i.e. int to float, not int to vec4.
  160.     */
  161.    to = glsl_type::get_instance(GLSL_TYPE_FLOAT, from->type->vector_elements,
  162.                                 from->type->matrix_columns);
  163.  
  164.    switch (from->type->base_type) {
  165.    case GLSL_TYPE_INT:
  166.       from = new(ctx) ir_expression(ir_unop_i2f, to, from, NULL);
  167.       break;
  168.    case GLSL_TYPE_UINT:
  169.       from = new(ctx) ir_expression(ir_unop_u2f, to, from, NULL);
  170.       break;
  171.    case GLSL_TYPE_BOOL:
  172.       from = new(ctx) ir_expression(ir_unop_b2f, to, from, NULL);
  173.       break;
  174.    default:
  175.       assert(0);
  176.    }
  177.  
  178.    return true;
  179. }
  180.  
  181.  
  182. static const struct glsl_type *
  183. arithmetic_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
  184.                        bool multiply,
  185.                        struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
  186. {
  187.    const glsl_type *type_a = value_a->type;
  188.    const glsl_type *type_b = value_b->type;
  189.  
  190.    /* From GLSL 1.50 spec, page 56:
  191.     *
  192.     *    "The arithmetic binary operators add (+), subtract (-),
  193.     *    multiply (*), and divide (/) operate on integer and
  194.     *    floating-point scalars, vectors, and matrices."
  195.     */
  196.    if (!type_a->is_numeric() || !type_b->is_numeric()) {
  197.       _mesa_glsl_error(loc, state,
  198.                        "Operands to arithmetic operators must be numeric");
  199.       return glsl_type::error_type;
  200.    }
  201.  
  202.  
  203.    /*    "If one operand is floating-point based and the other is
  204.     *    not, then the conversions from Section 4.1.10 "Implicit
  205.     *    Conversions" are applied to the non-floating-point-based operand."
  206.     */
  207.    if (!apply_implicit_conversion(type_a, value_b, state)
  208.        && !apply_implicit_conversion(type_b, value_a, state)) {
  209.       _mesa_glsl_error(loc, state,
  210.                        "Could not implicitly convert operands to "
  211.                        "arithmetic operator");
  212.       return glsl_type::error_type;
  213.    }
  214.    type_a = value_a->type;
  215.    type_b = value_b->type;
  216.  
  217.    /*    "If the operands are integer types, they must both be signed or
  218.     *    both be unsigned."
  219.     *
  220.     * From this rule and the preceeding conversion it can be inferred that
  221.     * both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT.
  222.     * The is_numeric check above already filtered out the case where either
  223.     * type is not one of these, so now the base types need only be tested for
  224.     * equality.
  225.     */
  226.    if (type_a->base_type != type_b->base_type) {
  227.       _mesa_glsl_error(loc, state,
  228.                        "base type mismatch for arithmetic operator");
  229.       return glsl_type::error_type;
  230.    }
  231.  
  232.    /*    "All arithmetic binary operators result in the same fundamental type
  233.     *    (signed integer, unsigned integer, or floating-point) as the
  234.     *    operands they operate on, after operand type conversion. After
  235.     *    conversion, the following cases are valid
  236.     *
  237.     *    * The two operands are scalars. In this case the operation is
  238.     *      applied, resulting in a scalar."
  239.     */
  240.    if (type_a->is_scalar() && type_b->is_scalar())
  241.       return type_a;
  242.  
  243.    /*   "* One operand is a scalar, and the other is a vector or matrix.
  244.     *      In this case, the scalar operation is applied independently to each
  245.     *      component of the vector or matrix, resulting in the same size
  246.     *      vector or matrix."
  247.     */
  248.    if (type_a->is_scalar()) {
  249.       if (!type_b->is_scalar())
  250.          return type_b;
  251.    } else if (type_b->is_scalar()) {
  252.       return type_a;
  253.    }
  254.  
  255.    /* All of the combinations of <scalar, scalar>, <vector, scalar>,
  256.     * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been
  257.     * handled.
  258.     */
  259.    assert(!type_a->is_scalar());
  260.    assert(!type_b->is_scalar());
  261.  
  262.    /*   "* The two operands are vectors of the same size. In this case, the
  263.     *      operation is done component-wise resulting in the same size
  264.     *      vector."
  265.     */
  266.    if (type_a->is_vector() && type_b->is_vector()) {
  267.       if (type_a == type_b) {
  268.          return type_a;
  269.       } else {
  270.          _mesa_glsl_error(loc, state,
  271.                           "vector size mismatch for arithmetic operator");
  272.          return glsl_type::error_type;
  273.       }
  274.    }
  275.  
  276.    /* All of the combinations of <scalar, scalar>, <vector, scalar>,
  277.     * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and
  278.     * <vector, vector> have been handled.  At least one of the operands must
  279.     * be matrix.  Further, since there are no integer matrix types, the base
  280.     * type of both operands must be float.
  281.     */
  282.    assert(type_a->is_matrix() || type_b->is_matrix());
  283.    assert(type_a->base_type == GLSL_TYPE_FLOAT);
  284.    assert(type_b->base_type == GLSL_TYPE_FLOAT);
  285.  
  286.    /*   "* The operator is add (+), subtract (-), or divide (/), and the
  287.     *      operands are matrices with the same number of rows and the same
  288.     *      number of columns. In this case, the operation is done component-
  289.     *      wise resulting in the same size matrix."
  290.     *    * The operator is multiply (*), where both operands are matrices or
  291.     *      one operand is a vector and the other a matrix. A right vector
  292.     *      operand is treated as a column vector and a left vector operand as a
  293.     *      row vector. In all these cases, it is required that the number of
  294.     *      columns of the left operand is equal to the number of rows of the
  295.     *      right operand. Then, the multiply (*) operation does a linear
  296.     *      algebraic multiply, yielding an object that has the same number of
  297.     *      rows as the left operand and the same number of columns as the right
  298.     *      operand. Section 5.10 "Vector and Matrix Operations" explains in
  299.     *      more detail how vectors and matrices are operated on."
  300.     */
  301.    if (! multiply) {
  302.       if (type_a == type_b)
  303.          return type_a;
  304.    } else {
  305.       if (type_a->is_matrix() && type_b->is_matrix()) {
  306.          /* Matrix multiply.  The columns of A must match the rows of B.  Given
  307.           * the other previously tested constraints, this means the vector type
  308.           * of a row from A must be the same as the vector type of a column from
  309.           * B.
  310.           */
  311.          if (type_a->row_type() == type_b->column_type()) {
  312.             /* The resulting matrix has the number of columns of matrix B and
  313.              * the number of rows of matrix A.  We get the row count of A by
  314.              * looking at the size of a vector that makes up a column.  The
  315.              * transpose (size of a row) is done for B.
  316.              */
  317.             const glsl_type *const type =
  318.                glsl_type::get_instance(type_a->base_type,
  319.                                        type_a->column_type()->vector_elements,
  320.                                        type_b->row_type()->vector_elements);
  321.             assert(type != glsl_type::error_type);
  322.  
  323.             return type;
  324.          }
  325.       } else if (type_a->is_matrix()) {
  326.          /* A is a matrix and B is a column vector.  Columns of A must match
  327.           * rows of B.  Given the other previously tested constraints, this
  328.           * means the vector type of a row from A must be the same as the
  329.           * vector the type of B.
  330.           */
  331.          if (type_a->row_type() == type_b) {
  332.             /* The resulting vector has a number of elements equal to
  333.              * the number of rows of matrix A. */
  334.             const glsl_type *const type =
  335.                glsl_type::get_instance(type_a->base_type,
  336.                                        type_a->column_type()->vector_elements,
  337.                                        1);
  338.             assert(type != glsl_type::error_type);
  339.  
  340.             return type;
  341.          }
  342.       } else {
  343.          assert(type_b->is_matrix());
  344.  
  345.          /* A is a row vector and B is a matrix.  Columns of A must match rows
  346.           * of B.  Given the other previously tested constraints, this means
  347.           * the type of A must be the same as the vector type of a column from
  348.           * B.
  349.           */
  350.          if (type_a == type_b->column_type()) {
  351.             /* The resulting vector has a number of elements equal to
  352.              * the number of columns of matrix B. */
  353.             const glsl_type *const type =
  354.                glsl_type::get_instance(type_a->base_type,
  355.                                        type_b->row_type()->vector_elements,
  356.                                        1);
  357.             assert(type != glsl_type::error_type);
  358.  
  359.             return type;
  360.          }
  361.       }
  362.  
  363.       _mesa_glsl_error(loc, state, "size mismatch for matrix multiplication");
  364.       return glsl_type::error_type;
  365.    }
  366.  
  367.  
  368.    /*    "All other cases are illegal."
  369.     */
  370.    _mesa_glsl_error(loc, state, "type mismatch");
  371.    return glsl_type::error_type;
  372. }
  373.  
  374.  
  375. static const struct glsl_type *
  376. unary_arithmetic_result_type(const struct glsl_type *type,
  377.                              struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
  378. {
  379.    /* From GLSL 1.50 spec, page 57:
  380.     *
  381.     *    "The arithmetic unary operators negate (-), post- and pre-increment
  382.     *     and decrement (-- and ++) operate on integer or floating-point
  383.     *     values (including vectors and matrices). All unary operators work
  384.     *     component-wise on their operands. These result with the same type
  385.     *     they operated on."
  386.     */
  387.    if (!type->is_numeric()) {
  388.       _mesa_glsl_error(loc, state,
  389.                        "Operands to arithmetic operators must be numeric");
  390.       return glsl_type::error_type;
  391.    }
  392.  
  393.    return type;
  394. }
  395.  
  396. /**
  397.  * \brief Return the result type of a bit-logic operation.
  398.  *
  399.  * If the given types to the bit-logic operator are invalid, return
  400.  * glsl_type::error_type.
  401.  *
  402.  * \param type_a Type of LHS of bit-logic op
  403.  * \param type_b Type of RHS of bit-logic op
  404.  */
  405. static const struct glsl_type *
  406. bit_logic_result_type(const struct glsl_type *type_a,
  407.                       const struct glsl_type *type_b,
  408.                       ast_operators op,
  409.                       struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
  410. {
  411.     if (!state->check_bitwise_operations_allowed(loc)) {
  412.        return glsl_type::error_type;
  413.     }
  414.  
  415.     /* From page 50 (page 56 of PDF) of GLSL 1.30 spec:
  416.      *
  417.      *     "The bitwise operators and (&), exclusive-or (^), and inclusive-or
  418.      *     (|). The operands must be of type signed or unsigned integers or
  419.      *     integer vectors."
  420.      */
  421.     if (!type_a->is_integer()) {
  422.        _mesa_glsl_error(loc, state, "LHS of `%s' must be an integer",
  423.                          ast_expression::operator_string(op));
  424.        return glsl_type::error_type;
  425.     }
  426.     if (!type_b->is_integer()) {
  427.        _mesa_glsl_error(loc, state, "RHS of `%s' must be an integer",
  428.                         ast_expression::operator_string(op));
  429.        return glsl_type::error_type;
  430.     }
  431.  
  432.     /*     "The fundamental types of the operands (signed or unsigned) must
  433.      *     match,"
  434.      */
  435.     if (type_a->base_type != type_b->base_type) {
  436.        _mesa_glsl_error(loc, state, "operands of `%s' must have the same "
  437.                         "base type", ast_expression::operator_string(op));
  438.        return glsl_type::error_type;
  439.     }
  440.  
  441.     /*     "The operands cannot be vectors of differing size." */
  442.     if (type_a->is_vector() &&
  443.         type_b->is_vector() &&
  444.         type_a->vector_elements != type_b->vector_elements) {
  445.        _mesa_glsl_error(loc, state, "operands of `%s' cannot be vectors of "
  446.                         "different sizes", ast_expression::operator_string(op));
  447.        return glsl_type::error_type;
  448.     }
  449.  
  450.     /*     "If one operand is a scalar and the other a vector, the scalar is
  451.      *     applied component-wise to the vector, resulting in the same type as
  452.      *     the vector. The fundamental types of the operands [...] will be the
  453.      *     resulting fundamental type."
  454.      */
  455.     if (type_a->is_scalar())
  456.         return type_b;
  457.     else
  458.         return type_a;
  459. }
  460.  
  461. static const struct glsl_type *
  462. modulus_result_type(const struct glsl_type *type_a,
  463.                     const struct glsl_type *type_b,
  464.                     struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
  465. {
  466.    if (!state->check_version(130, 300, loc, "operator '%%' is reserved")) {
  467.       return glsl_type::error_type;
  468.    }
  469.  
  470.    /* From GLSL 1.50 spec, page 56:
  471.     *    "The operator modulus (%) operates on signed or unsigned integers or
  472.     *    integer vectors. The operand types must both be signed or both be
  473.     *    unsigned."
  474.     */
  475.    if (!type_a->is_integer()) {
  476.       _mesa_glsl_error(loc, state, "LHS of operator %% must be an integer.");
  477.       return glsl_type::error_type;
  478.    }
  479.    if (!type_b->is_integer()) {
  480.       _mesa_glsl_error(loc, state, "RHS of operator %% must be an integer.");
  481.       return glsl_type::error_type;
  482.    }
  483.    if (type_a->base_type != type_b->base_type) {
  484.       _mesa_glsl_error(loc, state,
  485.                        "operands of %% must have the same base type");
  486.       return glsl_type::error_type;
  487.    }
  488.  
  489.    /*    "The operands cannot be vectors of differing size. If one operand is
  490.     *    a scalar and the other vector, then the scalar is applied component-
  491.     *    wise to the vector, resulting in the same type as the vector. If both
  492.     *    are vectors of the same size, the result is computed component-wise."
  493.     */
  494.    if (type_a->is_vector()) {
  495.       if (!type_b->is_vector()
  496.           || (type_a->vector_elements == type_b->vector_elements))
  497.          return type_a;
  498.    } else
  499.       return type_b;
  500.  
  501.    /*    "The operator modulus (%) is not defined for any other data types
  502.     *    (non-integer types)."
  503.     */
  504.    _mesa_glsl_error(loc, state, "type mismatch");
  505.    return glsl_type::error_type;
  506. }
  507.  
  508.  
  509. static const struct glsl_type *
  510. relational_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
  511.                        struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
  512. {
  513.    const glsl_type *type_a = value_a->type;
  514.    const glsl_type *type_b = value_b->type;
  515.  
  516.    /* From GLSL 1.50 spec, page 56:
  517.     *    "The relational operators greater than (>), less than (<), greater
  518.     *    than or equal (>=), and less than or equal (<=) operate only on
  519.     *    scalar integer and scalar floating-point expressions."
  520.     */
  521.    if (!type_a->is_numeric()
  522.        || !type_b->is_numeric()
  523.        || !type_a->is_scalar()
  524.        || !type_b->is_scalar()) {
  525.       _mesa_glsl_error(loc, state,
  526.                        "Operands to relational operators must be scalar and "
  527.                        "numeric");
  528.       return glsl_type::error_type;
  529.    }
  530.  
  531.    /*    "Either the operands' types must match, or the conversions from
  532.     *    Section 4.1.10 "Implicit Conversions" will be applied to the integer
  533.     *    operand, after which the types must match."
  534.     */
  535.    if (!apply_implicit_conversion(type_a, value_b, state)
  536.        && !apply_implicit_conversion(type_b, value_a, state)) {
  537.       _mesa_glsl_error(loc, state,
  538.                        "Could not implicitly convert operands to "
  539.                        "relational operator");
  540.       return glsl_type::error_type;
  541.    }
  542.    type_a = value_a->type;
  543.    type_b = value_b->type;
  544.  
  545.    if (type_a->base_type != type_b->base_type) {
  546.       _mesa_glsl_error(loc, state, "base type mismatch");
  547.       return glsl_type::error_type;
  548.    }
  549.  
  550.    /*    "The result is scalar Boolean."
  551.     */
  552.    return glsl_type::bool_type;
  553. }
  554.  
  555. /**
  556.  * \brief Return the result type of a bit-shift operation.
  557.  *
  558.  * If the given types to the bit-shift operator are invalid, return
  559.  * glsl_type::error_type.
  560.  *
  561.  * \param type_a Type of LHS of bit-shift op
  562.  * \param type_b Type of RHS of bit-shift op
  563.  */
  564. static const struct glsl_type *
  565. shift_result_type(const struct glsl_type *type_a,
  566.                   const struct glsl_type *type_b,
  567.                   ast_operators op,
  568.                   struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
  569. {
  570.    if (!state->check_bitwise_operations_allowed(loc)) {
  571.       return glsl_type::error_type;
  572.    }
  573.  
  574.    /* From page 50 (page 56 of the PDF) of the GLSL 1.30 spec:
  575.     *
  576.     *     "The shift operators (<<) and (>>). For both operators, the operands
  577.     *     must be signed or unsigned integers or integer vectors. One operand
  578.     *     can be signed while the other is unsigned."
  579.     */
  580.    if (!type_a->is_integer()) {
  581.       _mesa_glsl_error(loc, state, "LHS of operator %s must be an integer or "
  582.               "integer vector", ast_expression::operator_string(op));
  583.      return glsl_type::error_type;
  584.  
  585.    }
  586.    if (!type_b->is_integer()) {
  587.       _mesa_glsl_error(loc, state, "RHS of operator %s must be an integer or "
  588.               "integer vector", ast_expression::operator_string(op));
  589.      return glsl_type::error_type;
  590.    }
  591.  
  592.    /*     "If the first operand is a scalar, the second operand has to be
  593.     *     a scalar as well."
  594.     */
  595.    if (type_a->is_scalar() && !type_b->is_scalar()) {
  596.       _mesa_glsl_error(loc, state, "If the first operand of %s is scalar, the "
  597.               "second must be scalar as well",
  598.               ast_expression::operator_string(op));
  599.      return glsl_type::error_type;
  600.    }
  601.  
  602.    /* If both operands are vectors, check that they have same number of
  603.     * elements.
  604.     */
  605.    if (type_a->is_vector() &&
  606.       type_b->is_vector() &&
  607.       type_a->vector_elements != type_b->vector_elements) {
  608.       _mesa_glsl_error(loc, state, "Vector operands to operator %s must "
  609.               "have same number of elements",
  610.               ast_expression::operator_string(op));
  611.      return glsl_type::error_type;
  612.    }
  613.  
  614.    /*     "In all cases, the resulting type will be the same type as the left
  615.     *     operand."
  616.     */
  617.    return type_a;
  618. }
  619.  
  620. /**
  621.  * Validates that a value can be assigned to a location with a specified type
  622.  *
  623.  * Validates that \c rhs can be assigned to some location.  If the types are
  624.  * not an exact match but an automatic conversion is possible, \c rhs will be
  625.  * converted.
  626.  *
  627.  * \return
  628.  * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type.
  629.  * Otherwise the actual RHS to be assigned will be returned.  This may be
  630.  * \c rhs, or it may be \c rhs after some type conversion.
  631.  *
  632.  * \note
  633.  * In addition to being used for assignments, this function is used to
  634.  * type-check return values.
  635.  */
  636. ir_rvalue *
  637. validate_assignment(struct _mesa_glsl_parse_state *state,
  638.                     const glsl_type *lhs_type, ir_rvalue *rhs,
  639.                     bool is_initializer)
  640. {
  641.    /* If there is already some error in the RHS, just return it.  Anything
  642.     * else will lead to an avalanche of error message back to the user.
  643.     */
  644.    if (rhs->type->is_error())
  645.       return rhs;
  646.  
  647.    /* If the types are identical, the assignment can trivially proceed.
  648.     */
  649.    if (rhs->type == lhs_type)
  650.       return rhs;
  651.  
  652.    /* If the array element types are the same and the size of the LHS is zero,
  653.     * the assignment is okay for initializers embedded in variable
  654.     * declarations.
  655.     *
  656.     * Note: Whole-array assignments are not permitted in GLSL 1.10, but this
  657.     * is handled by ir_dereference::is_lvalue.
  658.     */
  659.    if (is_initializer && lhs_type->is_array() && rhs->type->is_array()
  660.        && (lhs_type->element_type() == rhs->type->element_type())
  661.        && (lhs_type->array_size() == 0)) {
  662.       return rhs;
  663.    }
  664.  
  665.    /* Check for implicit conversion in GLSL 1.20 */
  666.    if (apply_implicit_conversion(lhs_type, rhs, state)) {
  667.       if (rhs->type == lhs_type)
  668.          return rhs;
  669.    }
  670.  
  671.    return NULL;
  672. }
  673.  
  674. static void
  675. mark_whole_array_access(ir_rvalue *access)
  676. {
  677.    ir_dereference_variable *deref = access->as_dereference_variable();
  678.  
  679.    if (deref && deref->var) {
  680.       deref->var->max_array_access = deref->type->length - 1;
  681.    }
  682. }
  683.  
  684. ir_rvalue *
  685. do_assignment(exec_list *instructions, struct _mesa_glsl_parse_state *state,
  686.               const char *non_lvalue_description,
  687.               ir_rvalue *lhs, ir_rvalue *rhs, bool is_initializer,
  688.               YYLTYPE lhs_loc)
  689. {
  690.    void *ctx = state;
  691.    bool error_emitted = (lhs->type->is_error() || rhs->type->is_error());
  692.  
  693.    /* If the assignment LHS comes back as an ir_binop_vector_extract
  694.     * expression, move it to the RHS as an ir_triop_vector_insert.
  695.     */
  696.    if (lhs->ir_type == ir_type_expression) {
  697.       ir_expression *const expr = lhs->as_expression();
  698.  
  699.       if (unlikely(expr->operation == ir_binop_vector_extract)) {
  700.          ir_rvalue *new_rhs =
  701.             validate_assignment(state, lhs->type, rhs, is_initializer);
  702.  
  703.          if (new_rhs == NULL) {
  704.             _mesa_glsl_error(& lhs_loc, state, "type mismatch");
  705.             return lhs;
  706.          } else {
  707.             rhs = new(ctx) ir_expression(ir_triop_vector_insert,
  708.                                          expr->operands[0]->type,
  709.                                          expr->operands[0],
  710.                                          new_rhs,
  711.                                          expr->operands[1]);
  712.             lhs = expr->operands[0]->clone(ctx, NULL);
  713.          }
  714.       }
  715.    }
  716.  
  717.    ir_variable *lhs_var = lhs->variable_referenced();
  718.    if (lhs_var)
  719.       lhs_var->assigned = true;
  720.  
  721.    if (!error_emitted) {
  722.       if (non_lvalue_description != NULL) {
  723.          _mesa_glsl_error(&lhs_loc, state,
  724.                           "assignment to %s",
  725.                           non_lvalue_description);
  726.          error_emitted = true;
  727.       } else if (lhs->variable_referenced() != NULL
  728.                  && lhs->variable_referenced()->read_only) {
  729.          _mesa_glsl_error(&lhs_loc, state,
  730.                           "assignment to read-only variable '%s'",
  731.                           lhs->variable_referenced()->name);
  732.          error_emitted = true;
  733.  
  734.       } else if (lhs->type->is_array() &&
  735.                  !state->check_version(120, 300, &lhs_loc,
  736.                                        "whole array assignment forbidden")) {
  737.          /* From page 32 (page 38 of the PDF) of the GLSL 1.10 spec:
  738.           *
  739.           *    "Other binary or unary expressions, non-dereferenced
  740.           *     arrays, function names, swizzles with repeated fields,
  741.           *     and constants cannot be l-values."
  742.           *
  743.           * The restriction on arrays is lifted in GLSL 1.20 and GLSL ES 3.00.
  744.           */
  745.          error_emitted = true;
  746.       } else if (!lhs->is_lvalue()) {
  747.          _mesa_glsl_error(& lhs_loc, state, "non-lvalue in assignment");
  748.          error_emitted = true;
  749.       }
  750.    }
  751.  
  752.    ir_rvalue *new_rhs =
  753.       validate_assignment(state, lhs->type, rhs, is_initializer);
  754.    if (new_rhs == NULL) {
  755.       _mesa_glsl_error(& lhs_loc, state, "type mismatch");
  756.    } else {
  757.       rhs = new_rhs;
  758.  
  759.       /* If the LHS array was not declared with a size, it takes it size from
  760.        * the RHS.  If the LHS is an l-value and a whole array, it must be a
  761.        * dereference of a variable.  Any other case would require that the LHS
  762.        * is either not an l-value or not a whole array.
  763.        */
  764.       if (lhs->type->array_size() == 0) {
  765.          ir_dereference *const d = lhs->as_dereference();
  766.  
  767.          assert(d != NULL);
  768.  
  769.          ir_variable *const var = d->variable_referenced();
  770.  
  771.          assert(var != NULL);
  772.  
  773.          if (var->max_array_access >= unsigned(rhs->type->array_size())) {
  774.             /* FINISHME: This should actually log the location of the RHS. */
  775.             _mesa_glsl_error(& lhs_loc, state, "array size must be > %u due to "
  776.                              "previous access",
  777.                              var->max_array_access);
  778.          }
  779.  
  780.          var->type = glsl_type::get_array_instance(lhs->type->element_type(),
  781.                                                    rhs->type->array_size());
  782.          d->type = var->type;
  783.       }
  784.       mark_whole_array_access(rhs);
  785.       mark_whole_array_access(lhs);
  786.    }
  787.  
  788.    /* Most callers of do_assignment (assign, add_assign, pre_inc/dec,
  789.     * but not post_inc) need the converted assigned value as an rvalue
  790.     * to handle things like:
  791.     *
  792.     * i = j += 1;
  793.     *
  794.     * So we always just store the computed value being assigned to a
  795.     * temporary and return a deref of that temporary.  If the rvalue
  796.     * ends up not being used, the temp will get copy-propagated out.
  797.     */
  798.    ir_variable *var = new(ctx) ir_variable(rhs->type, "assignment_tmp",
  799.                                            ir_var_temporary);
  800.    ir_dereference_variable *deref_var = new(ctx) ir_dereference_variable(var);
  801.    instructions->push_tail(var);
  802.    instructions->push_tail(new(ctx) ir_assignment(deref_var, rhs));
  803.    deref_var = new(ctx) ir_dereference_variable(var);
  804.  
  805.    if (!error_emitted)
  806.       instructions->push_tail(new(ctx) ir_assignment(lhs, deref_var));
  807.  
  808.    return new(ctx) ir_dereference_variable(var);
  809. }
  810.  
  811. static ir_rvalue *
  812. get_lvalue_copy(exec_list *instructions, ir_rvalue *lvalue)
  813. {
  814.    void *ctx = ralloc_parent(lvalue);
  815.    ir_variable *var;
  816.  
  817.    var = new(ctx) ir_variable(lvalue->type, "_post_incdec_tmp",
  818.                               ir_var_temporary);
  819.    instructions->push_tail(var);
  820.    var->mode = ir_var_auto;
  821.  
  822.    instructions->push_tail(new(ctx) ir_assignment(new(ctx) ir_dereference_variable(var),
  823.                                                   lvalue));
  824.  
  825.    return new(ctx) ir_dereference_variable(var);
  826. }
  827.  
  828.  
  829. ir_rvalue *
  830. ast_node::hir(exec_list *instructions,
  831.               struct _mesa_glsl_parse_state *state)
  832. {
  833.    (void) instructions;
  834.    (void) state;
  835.  
  836.    return NULL;
  837. }
  838.  
  839. static ir_rvalue *
  840. do_comparison(void *mem_ctx, int operation, ir_rvalue *op0, ir_rvalue *op1)
  841. {
  842.    int join_op;
  843.    ir_rvalue *cmp = NULL;
  844.  
  845.    if (operation == ir_binop_all_equal)
  846.       join_op = ir_binop_logic_and;
  847.    else
  848.       join_op = ir_binop_logic_or;
  849.  
  850.    switch (op0->type->base_type) {
  851.    case GLSL_TYPE_FLOAT:
  852.    case GLSL_TYPE_UINT:
  853.    case GLSL_TYPE_INT:
  854.    case GLSL_TYPE_BOOL:
  855.       return new(mem_ctx) ir_expression(operation, op0, op1);
  856.  
  857.    case GLSL_TYPE_ARRAY: {
  858.       for (unsigned int i = 0; i < op0->type->length; i++) {
  859.          ir_rvalue *e0, *e1, *result;
  860.  
  861.          e0 = new(mem_ctx) ir_dereference_array(op0->clone(mem_ctx, NULL),
  862.                                                 new(mem_ctx) ir_constant(i));
  863.          e1 = new(mem_ctx) ir_dereference_array(op1->clone(mem_ctx, NULL),
  864.                                                 new(mem_ctx) ir_constant(i));
  865.          result = do_comparison(mem_ctx, operation, e0, e1);
  866.  
  867.          if (cmp) {
  868.             cmp = new(mem_ctx) ir_expression(join_op, cmp, result);
  869.          } else {
  870.             cmp = result;
  871.          }
  872.       }
  873.  
  874.       mark_whole_array_access(op0);
  875.       mark_whole_array_access(op1);
  876.       break;
  877.    }
  878.  
  879.    case GLSL_TYPE_STRUCT: {
  880.       for (unsigned int i = 0; i < op0->type->length; i++) {
  881.          ir_rvalue *e0, *e1, *result;
  882.          const char *field_name = op0->type->fields.structure[i].name;
  883.  
  884.          e0 = new(mem_ctx) ir_dereference_record(op0->clone(mem_ctx, NULL),
  885.                                                  field_name);
  886.          e1 = new(mem_ctx) ir_dereference_record(op1->clone(mem_ctx, NULL),
  887.                                                  field_name);
  888.          result = do_comparison(mem_ctx, operation, e0, e1);
  889.  
  890.          if (cmp) {
  891.             cmp = new(mem_ctx) ir_expression(join_op, cmp, result);
  892.          } else {
  893.             cmp = result;
  894.          }
  895.       }
  896.       break;
  897.    }
  898.  
  899.    case GLSL_TYPE_ERROR:
  900.    case GLSL_TYPE_VOID:
  901.    case GLSL_TYPE_SAMPLER:
  902.    case GLSL_TYPE_INTERFACE:
  903.       /* I assume a comparison of a struct containing a sampler just
  904.        * ignores the sampler present in the type.
  905.        */
  906.       break;
  907.    }
  908.  
  909.    if (cmp == NULL)
  910.       cmp = new(mem_ctx) ir_constant(true);
  911.  
  912.    return cmp;
  913. }
  914.  
  915. /* For logical operations, we want to ensure that the operands are
  916.  * scalar booleans.  If it isn't, emit an error and return a constant
  917.  * boolean to avoid triggering cascading error messages.
  918.  */
  919. ir_rvalue *
  920. get_scalar_boolean_operand(exec_list *instructions,
  921.                            struct _mesa_glsl_parse_state *state,
  922.                            ast_expression *parent_expr,
  923.                            int operand,
  924.                            const char *operand_name,
  925.                            bool *error_emitted)
  926. {
  927.    ast_expression *expr = parent_expr->subexpressions[operand];
  928.    void *ctx = state;
  929.    ir_rvalue *val = expr->hir(instructions, state);
  930.  
  931.    if (val->type->is_boolean() && val->type->is_scalar())
  932.       return val;
  933.  
  934.    if (!*error_emitted) {
  935.       YYLTYPE loc = expr->get_location();
  936.       _mesa_glsl_error(&loc, state, "%s of `%s' must be scalar boolean",
  937.                        operand_name,
  938.                        parent_expr->operator_string(parent_expr->oper));
  939.       *error_emitted = true;
  940.    }
  941.  
  942.    return new(ctx) ir_constant(true);
  943. }
  944.  
  945. /**
  946.  * If name refers to a builtin array whose maximum allowed size is less than
  947.  * size, report an error and return true.  Otherwise return false.
  948.  */
  949. void
  950. check_builtin_array_max_size(const char *name, unsigned size,
  951.                              YYLTYPE loc, struct _mesa_glsl_parse_state *state)
  952. {
  953.    if ((strcmp("gl_TexCoord", name) == 0)
  954.        && (size > state->Const.MaxTextureCoords)) {
  955.       /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
  956.        *
  957.        *     "The size [of gl_TexCoord] can be at most
  958.        *     gl_MaxTextureCoords."
  959.        */
  960.       _mesa_glsl_error(&loc, state, "`gl_TexCoord' array size cannot "
  961.                        "be larger than gl_MaxTextureCoords (%u)\n",
  962.                        state->Const.MaxTextureCoords);
  963.    } else if (strcmp("gl_ClipDistance", name) == 0
  964.               && size > state->Const.MaxClipPlanes) {
  965.       /* From section 7.1 (Vertex Shader Special Variables) of the
  966.        * GLSL 1.30 spec:
  967.        *
  968.        *   "The gl_ClipDistance array is predeclared as unsized and
  969.        *   must be sized by the shader either redeclaring it with a
  970.        *   size or indexing it only with integral constant
  971.        *   expressions. ... The size can be at most
  972.        *   gl_MaxClipDistances."
  973.        */
  974.       _mesa_glsl_error(&loc, state, "`gl_ClipDistance' array size cannot "
  975.                        "be larger than gl_MaxClipDistances (%u)\n",
  976.                        state->Const.MaxClipPlanes);
  977.    }
  978. }
  979.  
  980. /**
  981.  * Create the constant 1, of a which is appropriate for incrementing and
  982.  * decrementing values of the given GLSL type.  For example, if type is vec4,
  983.  * this creates a constant value of 1.0 having type float.
  984.  *
  985.  * If the given type is invalid for increment and decrement operators, return
  986.  * a floating point 1--the error will be detected later.
  987.  */
  988. static ir_rvalue *
  989. constant_one_for_inc_dec(void *ctx, const glsl_type *type)
  990. {
  991.    switch (type->base_type) {
  992.    case GLSL_TYPE_UINT:
  993.       return new(ctx) ir_constant((unsigned) 1);
  994.    case GLSL_TYPE_INT:
  995.       return new(ctx) ir_constant(1);
  996.    default:
  997.    case GLSL_TYPE_FLOAT:
  998.       return new(ctx) ir_constant(1.0f);
  999.    }
  1000. }
  1001.  
  1002. ir_rvalue *
  1003. ast_expression::hir(exec_list *instructions,
  1004.                     struct _mesa_glsl_parse_state *state)
  1005. {
  1006.    void *ctx = state;
  1007.    static const int operations[AST_NUM_OPERATORS] = {
  1008.       -1,               /* ast_assign doesn't convert to ir_expression. */
  1009.       -1,               /* ast_plus doesn't convert to ir_expression. */
  1010.       ir_unop_neg,
  1011.       ir_binop_add,
  1012.       ir_binop_sub,
  1013.       ir_binop_mul,
  1014.       ir_binop_div,
  1015.       ir_binop_mod,
  1016.       ir_binop_lshift,
  1017.       ir_binop_rshift,
  1018.       ir_binop_less,
  1019.       ir_binop_greater,
  1020.       ir_binop_lequal,
  1021.       ir_binop_gequal,
  1022.       ir_binop_all_equal,
  1023.       ir_binop_any_nequal,
  1024.       ir_binop_bit_and,
  1025.       ir_binop_bit_xor,
  1026.       ir_binop_bit_or,
  1027.       ir_unop_bit_not,
  1028.       ir_binop_logic_and,
  1029.       ir_binop_logic_xor,
  1030.       ir_binop_logic_or,
  1031.       ir_unop_logic_not,
  1032.  
  1033.       /* Note: The following block of expression types actually convert
  1034.        * to multiple IR instructions.
  1035.        */
  1036.       ir_binop_mul,     /* ast_mul_assign */
  1037.       ir_binop_div,     /* ast_div_assign */
  1038.       ir_binop_mod,     /* ast_mod_assign */
  1039.       ir_binop_add,     /* ast_add_assign */
  1040.       ir_binop_sub,     /* ast_sub_assign */
  1041.       ir_binop_lshift,  /* ast_ls_assign */
  1042.       ir_binop_rshift,  /* ast_rs_assign */
  1043.       ir_binop_bit_and, /* ast_and_assign */
  1044.       ir_binop_bit_xor, /* ast_xor_assign */
  1045.       ir_binop_bit_or,  /* ast_or_assign */
  1046.  
  1047.       -1,               /* ast_conditional doesn't convert to ir_expression. */
  1048.       ir_binop_add,     /* ast_pre_inc. */
  1049.       ir_binop_sub,     /* ast_pre_dec. */
  1050.       ir_binop_add,     /* ast_post_inc. */
  1051.       ir_binop_sub,     /* ast_post_dec. */
  1052.       -1,               /* ast_field_selection doesn't conv to ir_expression. */
  1053.       -1,               /* ast_array_index doesn't convert to ir_expression. */
  1054.       -1,               /* ast_function_call doesn't conv to ir_expression. */
  1055.       -1,               /* ast_identifier doesn't convert to ir_expression. */
  1056.       -1,               /* ast_int_constant doesn't convert to ir_expression. */
  1057.       -1,               /* ast_uint_constant doesn't conv to ir_expression. */
  1058.       -1,               /* ast_float_constant doesn't conv to ir_expression. */
  1059.       -1,               /* ast_bool_constant doesn't conv to ir_expression. */
  1060.       -1,               /* ast_sequence doesn't convert to ir_expression. */
  1061.    };
  1062.    ir_rvalue *result = NULL;
  1063.    ir_rvalue *op[3];
  1064.    const struct glsl_type *type; /* a temporary variable for switch cases */
  1065.    bool error_emitted = false;
  1066.    YYLTYPE loc;
  1067.  
  1068.    loc = this->get_location();
  1069.  
  1070.    switch (this->oper) {
  1071.    case ast_aggregate:
  1072.          assert(!"ast_aggregate: Should never get here.");
  1073.          break;
  1074.  
  1075.    case ast_assign: {
  1076.       op[0] = this->subexpressions[0]->hir(instructions, state);
  1077.       op[1] = this->subexpressions[1]->hir(instructions, state);
  1078.  
  1079.       result = do_assignment(instructions, state,
  1080.                              this->subexpressions[0]->non_lvalue_description,
  1081.                              op[0], op[1], false,
  1082.                              this->subexpressions[0]->get_location());
  1083.       error_emitted = result->type->is_error();
  1084.       break;
  1085.    }
  1086.  
  1087.    case ast_plus:
  1088.       op[0] = this->subexpressions[0]->hir(instructions, state);
  1089.  
  1090.       type = unary_arithmetic_result_type(op[0]->type, state, & loc);
  1091.  
  1092.       error_emitted = type->is_error();
  1093.  
  1094.       result = op[0];
  1095.       break;
  1096.  
  1097.    case ast_neg:
  1098.       op[0] = this->subexpressions[0]->hir(instructions, state);
  1099.  
  1100.       type = unary_arithmetic_result_type(op[0]->type, state, & loc);
  1101.  
  1102.       error_emitted = type->is_error();
  1103.  
  1104.       result = new(ctx) ir_expression(operations[this->oper], type,
  1105.                                       op[0], NULL);
  1106.       break;
  1107.  
  1108.    case ast_add:
  1109.    case ast_sub:
  1110.    case ast_mul:
  1111.    case ast_div:
  1112.       op[0] = this->subexpressions[0]->hir(instructions, state);
  1113.       op[1] = this->subexpressions[1]->hir(instructions, state);
  1114.  
  1115.       type = arithmetic_result_type(op[0], op[1],
  1116.                                     (this->oper == ast_mul),
  1117.                                     state, & loc);
  1118.       error_emitted = type->is_error();
  1119.  
  1120.       result = new(ctx) ir_expression(operations[this->oper], type,
  1121.                                       op[0], op[1]);
  1122.       break;
  1123.  
  1124.    case ast_mod:
  1125.       op[0] = this->subexpressions[0]->hir(instructions, state);
  1126.       op[1] = this->subexpressions[1]->hir(instructions, state);
  1127.  
  1128.       type = modulus_result_type(op[0]->type, op[1]->type, state, & loc);
  1129.  
  1130.       assert(operations[this->oper] == ir_binop_mod);
  1131.  
  1132.       result = new(ctx) ir_expression(operations[this->oper], type,
  1133.                                       op[0], op[1]);
  1134.       error_emitted = type->is_error();
  1135.       break;
  1136.  
  1137.    case ast_lshift:
  1138.    case ast_rshift:
  1139.        if (!state->check_bitwise_operations_allowed(&loc)) {
  1140.           error_emitted = true;
  1141.        }
  1142.  
  1143.        op[0] = this->subexpressions[0]->hir(instructions, state);
  1144.        op[1] = this->subexpressions[1]->hir(instructions, state);
  1145.        type = shift_result_type(op[0]->type, op[1]->type, this->oper, state,
  1146.                                 &loc);
  1147.        result = new(ctx) ir_expression(operations[this->oper], type,
  1148.                                        op[0], op[1]);
  1149.        error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
  1150.        break;
  1151.  
  1152.    case ast_less:
  1153.    case ast_greater:
  1154.    case ast_lequal:
  1155.    case ast_gequal:
  1156.       op[0] = this->subexpressions[0]->hir(instructions, state);
  1157.       op[1] = this->subexpressions[1]->hir(instructions, state);
  1158.  
  1159.       type = relational_result_type(op[0], op[1], state, & loc);
  1160.  
  1161.       /* The relational operators must either generate an error or result
  1162.        * in a scalar boolean.  See page 57 of the GLSL 1.50 spec.
  1163.        */
  1164.       assert(type->is_error()
  1165.              || ((type->base_type == GLSL_TYPE_BOOL)
  1166.                  && type->is_scalar()));
  1167.  
  1168.       result = new(ctx) ir_expression(operations[this->oper], type,
  1169.                                       op[0], op[1]);
  1170.       error_emitted = type->is_error();
  1171.       break;
  1172.  
  1173.    case ast_nequal:
  1174.    case ast_equal:
  1175.       op[0] = this->subexpressions[0]->hir(instructions, state);
  1176.       op[1] = this->subexpressions[1]->hir(instructions, state);
  1177.  
  1178.       /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec:
  1179.        *
  1180.        *    "The equality operators equal (==), and not equal (!=)
  1181.        *    operate on all types. They result in a scalar Boolean. If
  1182.        *    the operand types do not match, then there must be a
  1183.        *    conversion from Section 4.1.10 "Implicit Conversions"
  1184.        *    applied to one operand that can make them match, in which
  1185.        *    case this conversion is done."
  1186.        */
  1187.       if ((!apply_implicit_conversion(op[0]->type, op[1], state)
  1188.            && !apply_implicit_conversion(op[1]->type, op[0], state))
  1189.           || (op[0]->type != op[1]->type)) {
  1190.          _mesa_glsl_error(& loc, state, "operands of `%s' must have the same "
  1191.                           "type", (this->oper == ast_equal) ? "==" : "!=");
  1192.          error_emitted = true;
  1193.       } else if ((op[0]->type->is_array() || op[1]->type->is_array()) &&
  1194.                  !state->check_version(120, 300, &loc,
  1195.                                        "array comparisons forbidden")) {
  1196.          error_emitted = true;
  1197.       }
  1198.  
  1199.       if (error_emitted) {
  1200.          result = new(ctx) ir_constant(false);
  1201.       } else {
  1202.          result = do_comparison(ctx, operations[this->oper], op[0], op[1]);
  1203.          assert(result->type == glsl_type::bool_type);
  1204.       }
  1205.       break;
  1206.  
  1207.    case ast_bit_and:
  1208.    case ast_bit_xor:
  1209.    case ast_bit_or:
  1210.       op[0] = this->subexpressions[0]->hir(instructions, state);
  1211.       op[1] = this->subexpressions[1]->hir(instructions, state);
  1212.       type = bit_logic_result_type(op[0]->type, op[1]->type, this->oper,
  1213.                                    state, &loc);
  1214.       result = new(ctx) ir_expression(operations[this->oper], type,
  1215.                                       op[0], op[1]);
  1216.       error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
  1217.       break;
  1218.  
  1219.    case ast_bit_not:
  1220.       op[0] = this->subexpressions[0]->hir(instructions, state);
  1221.  
  1222.       if (!state->check_bitwise_operations_allowed(&loc)) {
  1223.          error_emitted = true;
  1224.       }
  1225.  
  1226.       if (!op[0]->type->is_integer()) {
  1227.          _mesa_glsl_error(&loc, state, "operand of `~' must be an integer");
  1228.          error_emitted = true;
  1229.       }
  1230.  
  1231.       type = error_emitted ? glsl_type::error_type : op[0]->type;
  1232.       result = new(ctx) ir_expression(ir_unop_bit_not, type, op[0], NULL);
  1233.       break;
  1234.  
  1235.    case ast_logic_and: {
  1236.       exec_list rhs_instructions;
  1237.       op[0] = get_scalar_boolean_operand(instructions, state, this, 0,
  1238.                                          "LHS", &error_emitted);
  1239.       op[1] = get_scalar_boolean_operand(&rhs_instructions, state, this, 1,
  1240.                                          "RHS", &error_emitted);
  1241.  
  1242.       if (rhs_instructions.is_empty()) {
  1243.          result = new(ctx) ir_expression(ir_binop_logic_and, op[0], op[1]);
  1244.          type = result->type;
  1245.       } else {
  1246.          ir_variable *const tmp = new(ctx) ir_variable(glsl_type::bool_type,
  1247.                                                        "and_tmp",
  1248.                                                        ir_var_temporary);
  1249.          instructions->push_tail(tmp);
  1250.  
  1251.          ir_if *const stmt = new(ctx) ir_if(op[0]);
  1252.          instructions->push_tail(stmt);
  1253.  
  1254.          stmt->then_instructions.append_list(&rhs_instructions);
  1255.          ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp);
  1256.          ir_assignment *const then_assign =
  1257.             new(ctx) ir_assignment(then_deref, op[1]);
  1258.          stmt->then_instructions.push_tail(then_assign);
  1259.  
  1260.          ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp);
  1261.          ir_assignment *const else_assign =
  1262.             new(ctx) ir_assignment(else_deref, new(ctx) ir_constant(false));
  1263.          stmt->else_instructions.push_tail(else_assign);
  1264.  
  1265.          result = new(ctx) ir_dereference_variable(tmp);
  1266.          type = tmp->type;
  1267.       }
  1268.       break;
  1269.    }
  1270.  
  1271.    case ast_logic_or: {
  1272.       exec_list rhs_instructions;
  1273.       op[0] = get_scalar_boolean_operand(instructions, state, this, 0,
  1274.                                          "LHS", &error_emitted);
  1275.       op[1] = get_scalar_boolean_operand(&rhs_instructions, state, this, 1,
  1276.                                          "RHS", &error_emitted);
  1277.  
  1278.       if (rhs_instructions.is_empty()) {
  1279.          result = new(ctx) ir_expression(ir_binop_logic_or, op[0], op[1]);
  1280.          type = result->type;
  1281.       } else {
  1282.          ir_variable *const tmp = new(ctx) ir_variable(glsl_type::bool_type,
  1283.                                                        "or_tmp",
  1284.                                                        ir_var_temporary);
  1285.          instructions->push_tail(tmp);
  1286.  
  1287.          ir_if *const stmt = new(ctx) ir_if(op[0]);
  1288.          instructions->push_tail(stmt);
  1289.  
  1290.          ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp);
  1291.          ir_assignment *const then_assign =
  1292.             new(ctx) ir_assignment(then_deref, new(ctx) ir_constant(true));
  1293.          stmt->then_instructions.push_tail(then_assign);
  1294.  
  1295.          stmt->else_instructions.append_list(&rhs_instructions);
  1296.          ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp);
  1297.          ir_assignment *const else_assign =
  1298.             new(ctx) ir_assignment(else_deref, op[1]);
  1299.          stmt->else_instructions.push_tail(else_assign);
  1300.  
  1301.          result = new(ctx) ir_dereference_variable(tmp);
  1302.          type = tmp->type;
  1303.       }
  1304.       break;
  1305.    }
  1306.  
  1307.    case ast_logic_xor:
  1308.       /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
  1309.        *
  1310.        *    "The logical binary operators and (&&), or ( | | ), and
  1311.        *     exclusive or (^^). They operate only on two Boolean
  1312.        *     expressions and result in a Boolean expression."
  1313.        */
  1314.       op[0] = get_scalar_boolean_operand(instructions, state, this, 0, "LHS",
  1315.                                          &error_emitted);
  1316.       op[1] = get_scalar_boolean_operand(instructions, state, this, 1, "RHS",
  1317.                                          &error_emitted);
  1318.  
  1319.       result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type,
  1320.                                       op[0], op[1]);
  1321.       break;
  1322.  
  1323.    case ast_logic_not:
  1324.       op[0] = get_scalar_boolean_operand(instructions, state, this, 0,
  1325.                                          "operand", &error_emitted);
  1326.  
  1327.       result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type,
  1328.                                       op[0], NULL);
  1329.       break;
  1330.  
  1331.    case ast_mul_assign:
  1332.    case ast_div_assign:
  1333.    case ast_add_assign:
  1334.    case ast_sub_assign: {
  1335.       op[0] = this->subexpressions[0]->hir(instructions, state);
  1336.       op[1] = this->subexpressions[1]->hir(instructions, state);
  1337.  
  1338.       type = arithmetic_result_type(op[0], op[1],
  1339.                                     (this->oper == ast_mul_assign),
  1340.                                     state, & loc);
  1341.  
  1342.       ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
  1343.                                                    op[0], op[1]);
  1344.  
  1345.       result = do_assignment(instructions, state,
  1346.                              this->subexpressions[0]->non_lvalue_description,
  1347.                              op[0]->clone(ctx, NULL), temp_rhs, false,
  1348.                              this->subexpressions[0]->get_location());
  1349.       error_emitted = (op[0]->type->is_error());
  1350.  
  1351.       /* GLSL 1.10 does not allow array assignment.  However, we don't have to
  1352.        * explicitly test for this because none of the binary expression
  1353.        * operators allow array operands either.
  1354.        */
  1355.  
  1356.       break;
  1357.    }
  1358.  
  1359.    case ast_mod_assign: {
  1360.       op[0] = this->subexpressions[0]->hir(instructions, state);
  1361.       op[1] = this->subexpressions[1]->hir(instructions, state);
  1362.  
  1363.       type = modulus_result_type(op[0]->type, op[1]->type, state, & loc);
  1364.  
  1365.       assert(operations[this->oper] == ir_binop_mod);
  1366.  
  1367.       ir_rvalue *temp_rhs;
  1368.       temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
  1369.                                         op[0], op[1]);
  1370.  
  1371.       result = do_assignment(instructions, state,
  1372.                              this->subexpressions[0]->non_lvalue_description,
  1373.                              op[0]->clone(ctx, NULL), temp_rhs, false,
  1374.                              this->subexpressions[0]->get_location());
  1375.       error_emitted = type->is_error();
  1376.       break;
  1377.    }
  1378.  
  1379.    case ast_ls_assign:
  1380.    case ast_rs_assign: {
  1381.       op[0] = this->subexpressions[0]->hir(instructions, state);
  1382.       op[1] = this->subexpressions[1]->hir(instructions, state);
  1383.       type = shift_result_type(op[0]->type, op[1]->type, this->oper, state,
  1384.                                &loc);
  1385.       ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper],
  1386.                                                    type, op[0], op[1]);
  1387.       result = do_assignment(instructions, state,
  1388.                              this->subexpressions[0]->non_lvalue_description,
  1389.                              op[0]->clone(ctx, NULL), temp_rhs, false,
  1390.                              this->subexpressions[0]->get_location());
  1391.       error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
  1392.       break;
  1393.    }
  1394.  
  1395.    case ast_and_assign:
  1396.    case ast_xor_assign:
  1397.    case ast_or_assign: {
  1398.       op[0] = this->subexpressions[0]->hir(instructions, state);
  1399.       op[1] = this->subexpressions[1]->hir(instructions, state);
  1400.       type = bit_logic_result_type(op[0]->type, op[1]->type, this->oper,
  1401.                                    state, &loc);
  1402.       ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper],
  1403.                                                    type, op[0], op[1]);
  1404.       result = do_assignment(instructions, state,
  1405.                              this->subexpressions[0]->non_lvalue_description,
  1406.                              op[0]->clone(ctx, NULL), temp_rhs, false,
  1407.                              this->subexpressions[0]->get_location());
  1408.       error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
  1409.       break;
  1410.    }
  1411.  
  1412.    case ast_conditional: {
  1413.       /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
  1414.        *
  1415.        *    "The ternary selection operator (?:). It operates on three
  1416.        *    expressions (exp1 ? exp2 : exp3). This operator evaluates the
  1417.        *    first expression, which must result in a scalar Boolean."
  1418.        */
  1419.       op[0] = get_scalar_boolean_operand(instructions, state, this, 0,
  1420.                                          "condition", &error_emitted);
  1421.  
  1422.       /* The :? operator is implemented by generating an anonymous temporary
  1423.        * followed by an if-statement.  The last instruction in each branch of
  1424.        * the if-statement assigns a value to the anonymous temporary.  This
  1425.        * temporary is the r-value of the expression.
  1426.        */
  1427.       exec_list then_instructions;
  1428.       exec_list else_instructions;
  1429.  
  1430.       op[1] = this->subexpressions[1]->hir(&then_instructions, state);
  1431.       op[2] = this->subexpressions[2]->hir(&else_instructions, state);
  1432.  
  1433.       /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
  1434.        *
  1435.        *     "The second and third expressions can be any type, as
  1436.        *     long their types match, or there is a conversion in
  1437.        *     Section 4.1.10 "Implicit Conversions" that can be applied
  1438.        *     to one of the expressions to make their types match. This
  1439.        *     resulting matching type is the type of the entire
  1440.        *     expression."
  1441.        */
  1442.       if ((!apply_implicit_conversion(op[1]->type, op[2], state)
  1443.            && !apply_implicit_conversion(op[2]->type, op[1], state))
  1444.           || (op[1]->type != op[2]->type)) {
  1445.          YYLTYPE loc = this->subexpressions[1]->get_location();
  1446.  
  1447.          _mesa_glsl_error(& loc, state, "Second and third operands of ?: "
  1448.                           "operator must have matching types.");
  1449.          error_emitted = true;
  1450.          type = glsl_type::error_type;
  1451.       } else {
  1452.          type = op[1]->type;
  1453.       }
  1454.  
  1455.       /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
  1456.        *
  1457.        *    "The second and third expressions must be the same type, but can
  1458.        *    be of any type other than an array."
  1459.        */
  1460.       if (type->is_array() &&
  1461.           !state->check_version(120, 300, &loc,
  1462.                                 "Second and third operands of ?: operator "
  1463.                                 "cannot be arrays")) {
  1464.          error_emitted = true;
  1465.       }
  1466.  
  1467.       ir_constant *cond_val = op[0]->constant_expression_value();
  1468.       ir_constant *then_val = op[1]->constant_expression_value();
  1469.       ir_constant *else_val = op[2]->constant_expression_value();
  1470.  
  1471.       if (then_instructions.is_empty()
  1472.           && else_instructions.is_empty()
  1473.           && (cond_val != NULL) && (then_val != NULL) && (else_val != NULL)) {
  1474.          result = (cond_val->value.b[0]) ? then_val : else_val;
  1475.       } else {
  1476.          ir_variable *const tmp =
  1477.             new(ctx) ir_variable(type, "conditional_tmp", ir_var_temporary);
  1478.          instructions->push_tail(tmp);
  1479.  
  1480.          ir_if *const stmt = new(ctx) ir_if(op[0]);
  1481.          instructions->push_tail(stmt);
  1482.  
  1483.          then_instructions.move_nodes_to(& stmt->then_instructions);
  1484.          ir_dereference *const then_deref =
  1485.             new(ctx) ir_dereference_variable(tmp);
  1486.          ir_assignment *const then_assign =
  1487.             new(ctx) ir_assignment(then_deref, op[1]);
  1488.          stmt->then_instructions.push_tail(then_assign);
  1489.  
  1490.          else_instructions.move_nodes_to(& stmt->else_instructions);
  1491.          ir_dereference *const else_deref =
  1492.             new(ctx) ir_dereference_variable(tmp);
  1493.          ir_assignment *const else_assign =
  1494.             new(ctx) ir_assignment(else_deref, op[2]);
  1495.          stmt->else_instructions.push_tail(else_assign);
  1496.  
  1497.          result = new(ctx) ir_dereference_variable(tmp);
  1498.       }
  1499.       break;
  1500.    }
  1501.  
  1502.    case ast_pre_inc:
  1503.    case ast_pre_dec: {
  1504.       this->non_lvalue_description = (this->oper == ast_pre_inc)
  1505.          ? "pre-increment operation" : "pre-decrement operation";
  1506.  
  1507.       op[0] = this->subexpressions[0]->hir(instructions, state);
  1508.       op[1] = constant_one_for_inc_dec(ctx, op[0]->type);
  1509.  
  1510.       type = arithmetic_result_type(op[0], op[1], false, state, & loc);
  1511.  
  1512.       ir_rvalue *temp_rhs;
  1513.       temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
  1514.                                         op[0], op[1]);
  1515.  
  1516.       result = do_assignment(instructions, state,
  1517.                              this->subexpressions[0]->non_lvalue_description,
  1518.                              op[0]->clone(ctx, NULL), temp_rhs, false,
  1519.                              this->subexpressions[0]->get_location());
  1520.       error_emitted = op[0]->type->is_error();
  1521.       break;
  1522.    }
  1523.  
  1524.    case ast_post_inc:
  1525.    case ast_post_dec: {
  1526.       this->non_lvalue_description = (this->oper == ast_post_inc)
  1527.          ? "post-increment operation" : "post-decrement operation";
  1528.       op[0] = this->subexpressions[0]->hir(instructions, state);
  1529.       op[1] = constant_one_for_inc_dec(ctx, op[0]->type);
  1530.  
  1531.       error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
  1532.  
  1533.       type = arithmetic_result_type(op[0], op[1], false, state, & loc);
  1534.  
  1535.       ir_rvalue *temp_rhs;
  1536.       temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
  1537.                                         op[0], op[1]);
  1538.  
  1539.       /* Get a temporary of a copy of the lvalue before it's modified.
  1540.        * This may get thrown away later.
  1541.        */
  1542.       result = get_lvalue_copy(instructions, op[0]->clone(ctx, NULL));
  1543.  
  1544.       (void)do_assignment(instructions, state,
  1545.                           this->subexpressions[0]->non_lvalue_description,
  1546.                           op[0]->clone(ctx, NULL), temp_rhs, false,
  1547.                           this->subexpressions[0]->get_location());
  1548.  
  1549.       error_emitted = op[0]->type->is_error();
  1550.       break;
  1551.    }
  1552.  
  1553.    case ast_field_selection:
  1554.       result = _mesa_ast_field_selection_to_hir(this, instructions, state);
  1555.       break;
  1556.  
  1557.    case ast_array_index: {
  1558.       YYLTYPE index_loc = subexpressions[1]->get_location();
  1559.  
  1560.       op[0] = subexpressions[0]->hir(instructions, state);
  1561.       op[1] = subexpressions[1]->hir(instructions, state);
  1562.  
  1563.       result = _mesa_ast_array_index_to_hir(ctx, state, op[0], op[1],
  1564.                                             loc, index_loc);
  1565.  
  1566.       if (result->type->is_error())
  1567.          error_emitted = true;
  1568.  
  1569.       break;
  1570.    }
  1571.  
  1572.    case ast_function_call:
  1573.       /* Should *NEVER* get here.  ast_function_call should always be handled
  1574.        * by ast_function_expression::hir.
  1575.        */
  1576.       assert(0);
  1577.       break;
  1578.  
  1579.    case ast_identifier: {
  1580.       /* ast_identifier can appear several places in a full abstract syntax
  1581.        * tree.  This particular use must be at location specified in the grammar
  1582.        * as 'variable_identifier'.
  1583.        */
  1584.       ir_variable *var =
  1585.          state->symbols->get_variable(this->primary_expression.identifier);
  1586.  
  1587.       if (var != NULL) {
  1588.          var->used = true;
  1589.          result = new(ctx) ir_dereference_variable(var);
  1590.       } else {
  1591.          _mesa_glsl_error(& loc, state, "`%s' undeclared",
  1592.                           this->primary_expression.identifier);
  1593.  
  1594.          result = ir_rvalue::error_value(ctx);
  1595.          error_emitted = true;
  1596.       }
  1597.       break;
  1598.    }
  1599.  
  1600.    case ast_int_constant:
  1601.       result = new(ctx) ir_constant(this->primary_expression.int_constant);
  1602.       break;
  1603.  
  1604.    case ast_uint_constant:
  1605.       result = new(ctx) ir_constant(this->primary_expression.uint_constant);
  1606.       break;
  1607.  
  1608.    case ast_float_constant:
  1609.       result = new(ctx) ir_constant(this->primary_expression.float_constant);
  1610.       break;
  1611.  
  1612.    case ast_bool_constant:
  1613.       result = new(ctx) ir_constant(bool(this->primary_expression.bool_constant));
  1614.       break;
  1615.  
  1616.    case ast_sequence: {
  1617.       /* It should not be possible to generate a sequence in the AST without
  1618.        * any expressions in it.
  1619.        */
  1620.       assert(!this->expressions.is_empty());
  1621.  
  1622.       /* The r-value of a sequence is the last expression in the sequence.  If
  1623.        * the other expressions in the sequence do not have side-effects (and
  1624.        * therefore add instructions to the instruction list), they get dropped
  1625.        * on the floor.
  1626.        */
  1627.       exec_node *previous_tail_pred = NULL;
  1628.       YYLTYPE previous_operand_loc = loc;
  1629.  
  1630.       foreach_list_typed (ast_node, ast, link, &this->expressions) {
  1631.          /* If one of the operands of comma operator does not generate any
  1632.           * code, we want to emit a warning.  At each pass through the loop
  1633.           * previous_tail_pred will point to the last instruction in the
  1634.           * stream *before* processing the previous operand.  Naturally,
  1635.           * instructions->tail_pred will point to the last instruction in the
  1636.           * stream *after* processing the previous operand.  If the two
  1637.           * pointers match, then the previous operand had no effect.
  1638.           *
  1639.           * The warning behavior here differs slightly from GCC.  GCC will
  1640.           * only emit a warning if none of the left-hand operands have an
  1641.           * effect.  However, it will emit a warning for each.  I believe that
  1642.           * there are some cases in C (especially with GCC extensions) where
  1643.           * it is useful to have an intermediate step in a sequence have no
  1644.           * effect, but I don't think these cases exist in GLSL.  Either way,
  1645.           * it would be a giant hassle to replicate that behavior.
  1646.           */
  1647.          if (previous_tail_pred == instructions->tail_pred) {
  1648.             _mesa_glsl_warning(&previous_operand_loc, state,
  1649.                                "left-hand operand of comma expression has "
  1650.                                "no effect");
  1651.          }
  1652.  
  1653.          /* tail_pred is directly accessed instead of using the get_tail()
  1654.           * method for performance reasons.  get_tail() has extra code to
  1655.           * return NULL when the list is empty.  We don't care about that
  1656.           * here, so using tail_pred directly is fine.
  1657.           */
  1658.          previous_tail_pred = instructions->tail_pred;
  1659.          previous_operand_loc = ast->get_location();
  1660.  
  1661.          result = ast->hir(instructions, state);
  1662.       }
  1663.  
  1664.       /* Any errors should have already been emitted in the loop above.
  1665.        */
  1666.       error_emitted = true;
  1667.       break;
  1668.    }
  1669.    }
  1670.    type = NULL; /* use result->type, not type. */
  1671.    assert(result != NULL);
  1672.  
  1673.    if (result->type->is_error() && !error_emitted)
  1674.       _mesa_glsl_error(& loc, state, "type mismatch");
  1675.  
  1676.    return result;
  1677. }
  1678.  
  1679.  
  1680. ir_rvalue *
  1681. ast_expression_statement::hir(exec_list *instructions,
  1682.                               struct _mesa_glsl_parse_state *state)
  1683. {
  1684.    /* It is possible to have expression statements that don't have an
  1685.     * expression.  This is the solitary semicolon:
  1686.     *
  1687.     * for (i = 0; i < 5; i++)
  1688.     *     ;
  1689.     *
  1690.     * In this case the expression will be NULL.  Test for NULL and don't do
  1691.     * anything in that case.
  1692.     */
  1693.    if (expression != NULL)
  1694.       expression->hir(instructions, state);
  1695.  
  1696.    /* Statements do not have r-values.
  1697.     */
  1698.    return NULL;
  1699. }
  1700.  
  1701.  
  1702. ir_rvalue *
  1703. ast_compound_statement::hir(exec_list *instructions,
  1704.                             struct _mesa_glsl_parse_state *state)
  1705. {
  1706.    if (new_scope)
  1707.       state->symbols->push_scope();
  1708.  
  1709.    foreach_list_typed (ast_node, ast, link, &this->statements)
  1710.       ast->hir(instructions, state);
  1711.  
  1712.    if (new_scope)
  1713.       state->symbols->pop_scope();
  1714.  
  1715.    /* Compound statements do not have r-values.
  1716.     */
  1717.    return NULL;
  1718. }
  1719.  
  1720.  
  1721. static const glsl_type *
  1722. process_array_type(YYLTYPE *loc, const glsl_type *base, ast_node *array_size,
  1723.                    struct _mesa_glsl_parse_state *state)
  1724. {
  1725.    unsigned length = 0;
  1726.  
  1727.    if (base == NULL)
  1728.       return glsl_type::error_type;
  1729.  
  1730.    /* From page 19 (page 25) of the GLSL 1.20 spec:
  1731.     *
  1732.     *     "Only one-dimensional arrays may be declared."
  1733.     */
  1734.    if (base->is_array()) {
  1735.       _mesa_glsl_error(loc, state,
  1736.                        "invalid array of `%s' (only one-dimensional arrays "
  1737.                        "may be declared)",
  1738.                        base->name);
  1739.       return glsl_type::error_type;
  1740.    }
  1741.  
  1742.    if (array_size != NULL) {
  1743.       exec_list dummy_instructions;
  1744.       ir_rvalue *const ir = array_size->hir(& dummy_instructions, state);
  1745.       YYLTYPE loc = array_size->get_location();
  1746.  
  1747.       if (ir != NULL) {
  1748.          if (!ir->type->is_integer()) {
  1749.             _mesa_glsl_error(& loc, state, "array size must be integer type");
  1750.          } else if (!ir->type->is_scalar()) {
  1751.             _mesa_glsl_error(& loc, state, "array size must be scalar type");
  1752.          } else {
  1753.             ir_constant *const size = ir->constant_expression_value();
  1754.  
  1755.             if (size == NULL) {
  1756.                _mesa_glsl_error(& loc, state, "array size must be a "
  1757.                                 "constant valued expression");
  1758.             } else if (size->value.i[0] <= 0) {
  1759.                _mesa_glsl_error(& loc, state, "array size must be > 0");
  1760.             } else {
  1761.                assert(size->type == ir->type);
  1762.                length = size->value.u[0];
  1763.  
  1764.                /* If the array size is const (and we've verified that
  1765.                 * it is) then no instructions should have been emitted
  1766.                 * when we converted it to HIR.  If they were emitted,
  1767.                 * then either the array size isn't const after all, or
  1768.                 * we are emitting unnecessary instructions.
  1769.                 */
  1770.                assert(dummy_instructions.is_empty());
  1771.             }
  1772.          }
  1773.       }
  1774.    }
  1775.  
  1776.    const glsl_type *array_type = glsl_type::get_array_instance(base, length);
  1777.    return array_type != NULL ? array_type : glsl_type::error_type;
  1778. }
  1779.  
  1780.  
  1781. const glsl_type *
  1782. ast_type_specifier::glsl_type(const char **name,
  1783.                               struct _mesa_glsl_parse_state *state) const
  1784. {
  1785.    const struct glsl_type *type;
  1786.  
  1787.    type = state->symbols->get_type(this->type_name);
  1788.    *name = this->type_name;
  1789.  
  1790.    if (this->is_array) {
  1791.       YYLTYPE loc = this->get_location();
  1792.       type = process_array_type(&loc, type, this->array_size, state);
  1793.    }
  1794.  
  1795.    return type;
  1796. }
  1797.  
  1798. const glsl_type *
  1799. ast_fully_specified_type::glsl_type(const char **name,
  1800.                                     struct _mesa_glsl_parse_state *state) const
  1801. {
  1802.    const struct glsl_type *type = this->specifier->glsl_type(name, state);
  1803.  
  1804.    if (type == NULL)
  1805.       return NULL;
  1806.  
  1807.    if (type->base_type == GLSL_TYPE_FLOAT
  1808.        && state->es_shader
  1809.        && state->target == fragment_shader
  1810.        && this->qualifier.precision == ast_precision_none
  1811.        && state->symbols->get_variable("#default precision") == NULL) {
  1812.       YYLTYPE loc = this->get_location();
  1813.       _mesa_glsl_error(&loc, state,
  1814.                        "no precision specified this scope for type `%s'",
  1815.                        type->name);
  1816.    }
  1817.  
  1818.    return type;
  1819. }
  1820.  
  1821. /**
  1822.  * Determine whether a toplevel variable declaration declares a varying.  This
  1823.  * function operates by examining the variable's mode and the shader target,
  1824.  * so it correctly identifies linkage variables regardless of whether they are
  1825.  * declared using the deprecated "varying" syntax or the new "in/out" syntax.
  1826.  *
  1827.  * Passing a non-toplevel variable declaration (e.g. a function parameter) to
  1828.  * this function will produce undefined results.
  1829.  */
  1830. static bool
  1831. is_varying_var(ir_variable *var, _mesa_glsl_parser_targets target)
  1832. {
  1833.    switch (target) {
  1834.    case vertex_shader:
  1835.       return var->mode == ir_var_shader_out;
  1836.    case fragment_shader:
  1837.       return var->mode == ir_var_shader_in;
  1838.    default:
  1839.       return var->mode == ir_var_shader_out || var->mode == ir_var_shader_in;
  1840.    }
  1841. }
  1842.  
  1843.  
  1844. /**
  1845.  * Matrix layout qualifiers are only allowed on certain types
  1846.  */
  1847. static void
  1848. validate_matrix_layout_for_type(struct _mesa_glsl_parse_state *state,
  1849.                                 YYLTYPE *loc,
  1850.                                 const glsl_type *type,
  1851.                                 ir_variable *var)
  1852. {
  1853.    if (var && !var->is_in_uniform_block()) {
  1854.       /* Layout qualifiers may only apply to interface blocks and fields in
  1855.        * them.
  1856.        */
  1857.       _mesa_glsl_error(loc, state,
  1858.                        "uniform block layout qualifiers row_major and "
  1859.                        "column_major may not be applied to variables "
  1860.                        "outside of uniform blocks");
  1861.    } else if (!type->is_matrix()) {
  1862.       /* The OpenGL ES 3.0 conformance tests did not originally allow
  1863.        * matrix layout qualifiers on non-matrices.  However, the OpenGL
  1864.        * 4.4 and OpenGL ES 3.0 (revision TBD) specifications were
  1865.        * amended to specifically allow these layouts on all types.  Emit
  1866.        * a warning so that people know their code may not be portable.
  1867.        */
  1868.       _mesa_glsl_warning(loc, state,
  1869.                          "uniform block layout qualifiers row_major and "
  1870.                          "column_major applied to non-matrix types may "
  1871.                          "be rejected by older compilers");
  1872.    } else if (type->is_record()) {
  1873.       /* We allow 'layout(row_major)' on structure types because it's the only
  1874.        * way to get row-major layouts on matrices contained in structures.
  1875.        */
  1876.       _mesa_glsl_warning(loc, state,
  1877.                          "uniform block layout qualifiers row_major and "
  1878.                          "column_major applied to structure types is not "
  1879.                          "strictly conformant and may be rejected by other "
  1880.                          "compilers");
  1881.    }
  1882. }
  1883.  
  1884. static bool
  1885. validate_binding_qualifier(struct _mesa_glsl_parse_state *state,
  1886.                            YYLTYPE *loc,
  1887.                            ir_variable *var,
  1888.                            const ast_type_qualifier *qual)
  1889. {
  1890.    if (var->mode != ir_var_uniform) {
  1891.       _mesa_glsl_error(loc, state,
  1892.                        "the \"binding\" qualifier only applies to uniforms.\n");
  1893.       return false;
  1894.    }
  1895.  
  1896.    if (qual->binding < 0) {
  1897.       _mesa_glsl_error(loc, state, "binding values must be >= 0.\n");
  1898.       return false;
  1899.    }
  1900.  
  1901.    const struct gl_context *const ctx = state->ctx;
  1902.    unsigned elements = var->type->is_array() ? var->type->length : 1;
  1903.    unsigned max_index = qual->binding + elements - 1;
  1904.  
  1905.    if (var->type->is_interface()) {
  1906.       /* UBOs.  From page 60 of the GLSL 4.20 specification:
  1907.        * "If the binding point for any uniform block instance is less than zero,
  1908.        *  or greater than or equal to the implementation-dependent maximum
  1909.        *  number of uniform buffer bindings, a compilation error will occur.
  1910.        *  When the binding identifier is used with a uniform block instanced as
  1911.        *  an array of size N, all elements of the array from binding through
  1912.        *  binding + N – 1 must be within this range."
  1913.        *
  1914.        * The implementation-dependent maximum is GL_MAX_UNIFORM_BUFFER_BINDINGS.
  1915.        */
  1916.       if (max_index >= ctx->Const.MaxUniformBufferBindings) {
  1917.          _mesa_glsl_error(loc, state, "layout(binding = %d) for %d UBOs exceeds "
  1918.                           "the maximum number of UBO binding points (%d).\n",
  1919.                           qual->binding, elements,
  1920.                           ctx->Const.MaxUniformBufferBindings);
  1921.          return false;
  1922.       }
  1923.    } else if (var->type->is_sampler() ||
  1924.               (var->type->is_array() && var->type->fields.array->is_sampler())) {
  1925.       /* Samplers.  From page 63 of the GLSL 4.20 specification:
  1926.        * "If the binding is less than zero, or greater than or equal to the
  1927.        *  implementation-dependent maximum supported number of units, a
  1928.        *  compilation error will occur. When the binding identifier is used
  1929.        *  with an array of size N, all elements of the array from binding
  1930.        *  through binding + N - 1 must be within this range."
  1931.        */
  1932.       unsigned limit;
  1933.       switch (state->target) {
  1934.       case vertex_shader:
  1935.          limit = ctx->Const.VertexProgram.MaxTextureImageUnits;
  1936.          break;
  1937.       case geometry_shader:
  1938.          limit = ctx->Const.GeometryProgram.MaxTextureImageUnits;
  1939.          break;
  1940.       case fragment_shader:
  1941.          limit = ctx->Const.FragmentProgram.MaxTextureImageUnits;
  1942.          break;
  1943.       }
  1944.  
  1945.       if (max_index >= limit) {
  1946.          _mesa_glsl_error(loc, state, "layout(binding = %d) for %d samplers "
  1947.                           "exceeds the maximum number of texture image units "
  1948.                           "(%d).\n", qual->binding, elements, limit);
  1949.  
  1950.          return false;
  1951.       }
  1952.    } else {
  1953.       _mesa_glsl_error(loc, state,
  1954.                        "the \"binding\" qualifier only applies to uniform "
  1955.                        "blocks, samplers, or arrays of samplers.\n");
  1956.       return false;
  1957.    }
  1958.  
  1959.    return true;
  1960. }
  1961.  
  1962. static void
  1963. apply_type_qualifier_to_variable(const struct ast_type_qualifier *qual,
  1964.                                  ir_variable *var,
  1965.                                  struct _mesa_glsl_parse_state *state,
  1966.                                  YYLTYPE *loc,
  1967.                                  bool is_parameter)
  1968. {
  1969.    if (qual->flags.q.invariant) {
  1970.       if (var->used) {
  1971.          _mesa_glsl_error(loc, state,
  1972.                           "variable `%s' may not be redeclared "
  1973.                           "`invariant' after being used",
  1974.                           var->name);
  1975.       } else {
  1976.          var->invariant = 1;
  1977.       }
  1978.    }
  1979.  
  1980.    if (qual->flags.q.constant || qual->flags.q.attribute
  1981.        || qual->flags.q.uniform
  1982.        || (qual->flags.q.varying && (state->target == fragment_shader)))
  1983.       var->read_only = 1;
  1984.  
  1985.    if (qual->flags.q.centroid)
  1986.       var->centroid = 1;
  1987.  
  1988.    if (qual->flags.q.attribute && state->target != vertex_shader) {
  1989.       var->type = glsl_type::error_type;
  1990.       _mesa_glsl_error(loc, state,
  1991.                        "`attribute' variables may not be declared in the "
  1992.                        "%s shader",
  1993.                        _mesa_glsl_shader_target_name(state->target));
  1994.    }
  1995.  
  1996.    /* Section 6.1.1 (Function Calling Conventions) of the GLSL 1.10 spec says:
  1997.     *
  1998.     *     "However, the const qualifier cannot be used with out or inout."
  1999.     *
  2000.     * The same section of the GLSL 4.40 spec further clarifies this saying:
  2001.     *
  2002.     *     "The const qualifier cannot be used with out or inout, or a
  2003.     *     compile-time error results."
  2004.     */
  2005.    if (is_parameter && qual->flags.q.constant && qual->flags.q.out) {
  2006.       _mesa_glsl_error(loc, state,
  2007.                        "`const' may not be applied to `out' or `inout' "
  2008.                        "function parameters");
  2009.    }
  2010.  
  2011.    /* If there is no qualifier that changes the mode of the variable, leave
  2012.     * the setting alone.
  2013.     */
  2014.    if (qual->flags.q.in && qual->flags.q.out)
  2015.       var->mode = ir_var_function_inout;
  2016.    else if (qual->flags.q.in)
  2017.       var->mode = is_parameter ? ir_var_function_in : ir_var_shader_in;
  2018.    else if (qual->flags.q.attribute
  2019.             || (qual->flags.q.varying && (state->target == fragment_shader)))
  2020.       var->mode = ir_var_shader_in;
  2021.    else if (qual->flags.q.out)
  2022.       var->mode = is_parameter ? ir_var_function_out : ir_var_shader_out;
  2023.    else if (qual->flags.q.varying && (state->target == vertex_shader))
  2024.       var->mode = ir_var_shader_out;
  2025.    else if (qual->flags.q.uniform)
  2026.       var->mode = ir_var_uniform;
  2027.  
  2028.    if (!is_parameter && is_varying_var(var, state->target)) {
  2029.       /* This variable is being used to link data between shader stages (in
  2030.        * pre-glsl-1.30 parlance, it's a "varying").  Check that it has a type
  2031.        * that is allowed for such purposes.
  2032.        *
  2033.        * From page 25 (page 31 of the PDF) of the GLSL 1.10 spec:
  2034.        *
  2035.        *     "The varying qualifier can be used only with the data types
  2036.        *     float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of
  2037.        *     these."
  2038.        *
  2039.        * This was relaxed in GLSL version 1.30 and GLSL ES version 3.00.  From
  2040.        * page 31 (page 37 of the PDF) of the GLSL 1.30 spec:
  2041.        *
  2042.        *     "Fragment inputs can only be signed and unsigned integers and
  2043.        *     integer vectors, float, floating-point vectors, matrices, or
  2044.        *     arrays of these. Structures cannot be input.
  2045.        *
  2046.        * Similar text exists in the section on vertex shader outputs.
  2047.        *
  2048.        * Similar text exists in the GLSL ES 3.00 spec, except that the GLSL ES
  2049.        * 3.00 spec allows structs as well.  Varying structs are also allowed
  2050.        * in GLSL 1.50.
  2051.        */
  2052.       switch (var->type->get_scalar_type()->base_type) {
  2053.       case GLSL_TYPE_FLOAT:
  2054.          /* Ok in all GLSL versions */
  2055.          break;
  2056.       case GLSL_TYPE_UINT:
  2057.       case GLSL_TYPE_INT:
  2058.          if (state->is_version(130, 300))
  2059.             break;
  2060.          _mesa_glsl_error(loc, state,
  2061.                           "varying variables must be of base type float in %s",
  2062.                           state->get_version_string());
  2063.          break;
  2064.       case GLSL_TYPE_STRUCT:
  2065.          if (state->is_version(150, 300))
  2066.             break;
  2067.          _mesa_glsl_error(loc, state,
  2068.                           "varying variables may not be of type struct");
  2069.          break;
  2070.       default:
  2071.          _mesa_glsl_error(loc, state, "illegal type for a varying variable");
  2072.          break;
  2073.       }
  2074.    }
  2075.  
  2076.    if (state->all_invariant && (state->current_function == NULL)) {
  2077.       switch (state->target) {
  2078.       case vertex_shader:
  2079.          if (var->mode == ir_var_shader_out)
  2080.             var->invariant = true;
  2081.          break;
  2082.       case geometry_shader:
  2083.          if ((var->mode == ir_var_shader_in)
  2084.              || (var->mode == ir_var_shader_out))
  2085.             var->invariant = true;
  2086.          break;
  2087.       case fragment_shader:
  2088.          if (var->mode == ir_var_shader_in)
  2089.             var->invariant = true;
  2090.          break;
  2091.       }
  2092.    }
  2093.  
  2094.    if (qual->flags.q.flat)
  2095.       var->interpolation = INTERP_QUALIFIER_FLAT;
  2096.    else if (qual->flags.q.noperspective)
  2097.       var->interpolation = INTERP_QUALIFIER_NOPERSPECTIVE;
  2098.    else if (qual->flags.q.smooth)
  2099.       var->interpolation = INTERP_QUALIFIER_SMOOTH;
  2100.    else
  2101.       var->interpolation = INTERP_QUALIFIER_NONE;
  2102.  
  2103.    if (var->interpolation != INTERP_QUALIFIER_NONE &&
  2104.        !(state->target == vertex_shader && var->mode == ir_var_shader_out) &&
  2105.        !(state->target == fragment_shader && var->mode == ir_var_shader_in)) {
  2106.       _mesa_glsl_error(loc, state,
  2107.                        "interpolation qualifier `%s' can only be applied to "
  2108.                        "vertex shader outputs and fragment shader inputs.",
  2109.                        var->interpolation_string());
  2110.    }
  2111.  
  2112.    var->pixel_center_integer = qual->flags.q.pixel_center_integer;
  2113.    var->origin_upper_left = qual->flags.q.origin_upper_left;
  2114.    if ((qual->flags.q.origin_upper_left || qual->flags.q.pixel_center_integer)
  2115.        && (strcmp(var->name, "gl_FragCoord") != 0)) {
  2116.       const char *const qual_string = (qual->flags.q.origin_upper_left)
  2117.          ? "origin_upper_left" : "pixel_center_integer";
  2118.  
  2119.       _mesa_glsl_error(loc, state,
  2120.                        "layout qualifier `%s' can only be applied to "
  2121.                        "fragment shader input `gl_FragCoord'",
  2122.                        qual_string);
  2123.    }
  2124.  
  2125.    if (qual->flags.q.explicit_location) {
  2126.       const bool global_scope = (state->current_function == NULL);
  2127.       bool fail = false;
  2128.       const char *string = "";
  2129.  
  2130.       /* In the vertex shader only shader inputs can be given explicit
  2131.        * locations.
  2132.        *
  2133.        * In the fragment shader only shader outputs can be given explicit
  2134.        * locations.
  2135.        */
  2136.       switch (state->target) {
  2137.       case vertex_shader:
  2138.          if (!global_scope || (var->mode != ir_var_shader_in)) {
  2139.             fail = true;
  2140.             string = "input";
  2141.          }
  2142.          break;
  2143.  
  2144.       case geometry_shader:
  2145.          _mesa_glsl_error(loc, state,
  2146.                           "geometry shader variables cannot be given "
  2147.                           "explicit locations\n");
  2148.          break;
  2149.  
  2150.       case fragment_shader:
  2151.          if (!global_scope || (var->mode != ir_var_shader_out)) {
  2152.             fail = true;
  2153.             string = "output";
  2154.          }
  2155.          break;
  2156.       };
  2157.  
  2158.       if (fail) {
  2159.          _mesa_glsl_error(loc, state,
  2160.                           "only %s shader %s variables can be given an "
  2161.                           "explicit location\n",
  2162.                           _mesa_glsl_shader_target_name(state->target),
  2163.                           string);
  2164.       } else {
  2165.          var->explicit_location = true;
  2166.  
  2167.          /* This bit of silliness is needed because invalid explicit locations
  2168.           * are supposed to be flagged during linking.  Small negative values
  2169.           * biased by VERT_ATTRIB_GENERIC0 or FRAG_RESULT_DATA0 could alias
  2170.           * built-in values (e.g., -16+VERT_ATTRIB_GENERIC0 = VERT_ATTRIB_POS).
  2171.           * The linker needs to be able to differentiate these cases.  This
  2172.           * ensures that negative values stay negative.
  2173.           */
  2174.          if (qual->location >= 0) {
  2175.             var->location = (state->target == vertex_shader)
  2176.                ? (qual->location + VERT_ATTRIB_GENERIC0)
  2177.                : (qual->location + FRAG_RESULT_DATA0);
  2178.          } else {
  2179.             var->location = qual->location;
  2180.          }
  2181.  
  2182.          if (qual->flags.q.explicit_index) {
  2183.             /* From the GLSL 4.30 specification, section 4.4.2 (Output
  2184.              * Layout Qualifiers):
  2185.              *
  2186.              * "It is also a compile-time error if a fragment shader
  2187.              *  sets a layout index to less than 0 or greater than 1."
  2188.              *
  2189.              * Older specifications don't mandate a behavior; we take
  2190.              * this as a clarification and always generate the error.
  2191.              */
  2192.             if (qual->index < 0 || qual->index > 1) {
  2193.                _mesa_glsl_error(loc, state,
  2194.                                 "explicit index may only be 0 or 1\n");
  2195.             } else {
  2196.                var->explicit_index = true;
  2197.                var->index = qual->index;
  2198.             }
  2199.          }
  2200.       }
  2201.    } else if (qual->flags.q.explicit_index) {
  2202.          _mesa_glsl_error(loc, state,
  2203.                           "explicit index requires explicit location\n");
  2204.    }
  2205.  
  2206.    if (qual->flags.q.explicit_binding &&
  2207.        validate_binding_qualifier(state, loc, var, qual)) {
  2208.       var->explicit_binding = true;
  2209.       var->binding = qual->binding;
  2210.    }
  2211.  
  2212.    /* Does the declaration use the deprecated 'attribute' or 'varying'
  2213.     * keywords?
  2214.     */
  2215.    const bool uses_deprecated_qualifier = qual->flags.q.attribute
  2216.       || qual->flags.q.varying;
  2217.  
  2218.    /* Is the 'layout' keyword used with parameters that allow relaxed checking.
  2219.     * Many implementations of GL_ARB_fragment_coord_conventions_enable and some
  2220.     * implementations (only Mesa?) GL_ARB_explicit_attrib_location_enable
  2221.     * allowed the layout qualifier to be used with 'varying' and 'attribute'.
  2222.     * These extensions and all following extensions that add the 'layout'
  2223.     * keyword have been modified to require the use of 'in' or 'out'.
  2224.     *
  2225.     * The following extension do not allow the deprecated keywords:
  2226.     *
  2227.     *    GL_AMD_conservative_depth
  2228.     *    GL_ARB_conservative_depth
  2229.     *    GL_ARB_gpu_shader5
  2230.     *    GL_ARB_separate_shader_objects
  2231.     *    GL_ARB_tesselation_shader
  2232.     *    GL_ARB_transform_feedback3
  2233.     *    GL_ARB_uniform_buffer_object
  2234.     *
  2235.     * It is unknown whether GL_EXT_shader_image_load_store or GL_NV_gpu_shader5
  2236.     * allow layout with the deprecated keywords.
  2237.     */
  2238.    const bool relaxed_layout_qualifier_checking =
  2239.       state->ARB_fragment_coord_conventions_enable;
  2240.  
  2241.    if (qual->has_layout() && uses_deprecated_qualifier) {
  2242.       if (relaxed_layout_qualifier_checking) {
  2243.          _mesa_glsl_warning(loc, state,
  2244.                             "`layout' qualifier may not be used with "
  2245.                             "`attribute' or `varying'");
  2246.       } else {
  2247.          _mesa_glsl_error(loc, state,
  2248.                           "`layout' qualifier may not be used with "
  2249.                           "`attribute' or `varying'");
  2250.       }
  2251.    }
  2252.  
  2253.    /* Layout qualifiers for gl_FragDepth, which are enabled by extension
  2254.     * AMD_conservative_depth.
  2255.     */
  2256.    int depth_layout_count = qual->flags.q.depth_any
  2257.       + qual->flags.q.depth_greater
  2258.       + qual->flags.q.depth_less
  2259.       + qual->flags.q.depth_unchanged;
  2260.    if (depth_layout_count > 0
  2261.        && !state->AMD_conservative_depth_enable
  2262.        && !state->ARB_conservative_depth_enable) {
  2263.        _mesa_glsl_error(loc, state,
  2264.                         "extension GL_AMD_conservative_depth or "
  2265.                         "GL_ARB_conservative_depth must be enabled "
  2266.                         "to use depth layout qualifiers");
  2267.    } else if (depth_layout_count > 0
  2268.               && strcmp(var->name, "gl_FragDepth") != 0) {
  2269.        _mesa_glsl_error(loc, state,
  2270.                         "depth layout qualifiers can be applied only to "
  2271.                         "gl_FragDepth");
  2272.    } else if (depth_layout_count > 1
  2273.               && strcmp(var->name, "gl_FragDepth") == 0) {
  2274.       _mesa_glsl_error(loc, state,
  2275.                        "at most one depth layout qualifier can be applied to "
  2276.                        "gl_FragDepth");
  2277.    }
  2278.    if (qual->flags.q.depth_any)
  2279.       var->depth_layout = ir_depth_layout_any;
  2280.    else if (qual->flags.q.depth_greater)
  2281.       var->depth_layout = ir_depth_layout_greater;
  2282.    else if (qual->flags.q.depth_less)
  2283.       var->depth_layout = ir_depth_layout_less;
  2284.    else if (qual->flags.q.depth_unchanged)
  2285.        var->depth_layout = ir_depth_layout_unchanged;
  2286.    else
  2287.        var->depth_layout = ir_depth_layout_none;
  2288.  
  2289.    if (qual->flags.q.std140 ||
  2290.        qual->flags.q.packed ||
  2291.        qual->flags.q.shared) {
  2292.       _mesa_glsl_error(loc, state,
  2293.                        "uniform block layout qualifiers std140, packed, and "
  2294.                        "shared can only be applied to uniform blocks, not "
  2295.                        "members");
  2296.    }
  2297.  
  2298.    if (qual->flags.q.row_major || qual->flags.q.column_major) {
  2299.       validate_matrix_layout_for_type(state, loc, var->type, var);
  2300.    }
  2301. }
  2302.  
  2303. /**
  2304.  * Get the variable that is being redeclared by this declaration
  2305.  *
  2306.  * Semantic checks to verify the validity of the redeclaration are also
  2307.  * performed.  If semantic checks fail, compilation error will be emitted via
  2308.  * \c _mesa_glsl_error, but a non-\c NULL pointer will still be returned.
  2309.  *
  2310.  * \returns
  2311.  * A pointer to an existing variable in the current scope if the declaration
  2312.  * is a redeclaration, \c NULL otherwise.
  2313.  */
  2314. ir_variable *
  2315. get_variable_being_redeclared(ir_variable *var, ast_declaration *decl,
  2316.                               struct _mesa_glsl_parse_state *state)
  2317. {
  2318.    /* Check if this declaration is actually a re-declaration, either to
  2319.     * resize an array or add qualifiers to an existing variable.
  2320.     *
  2321.     * This is allowed for variables in the current scope, or when at
  2322.     * global scope (for built-ins in the implicit outer scope).
  2323.     */
  2324.    ir_variable *earlier = state->symbols->get_variable(decl->identifier);
  2325.    if (earlier == NULL ||
  2326.        (state->current_function != NULL &&
  2327.         !state->symbols->name_declared_this_scope(decl->identifier))) {
  2328.       return NULL;
  2329.    }
  2330.  
  2331.  
  2332.    YYLTYPE loc = decl->get_location();
  2333.  
  2334.    /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec,
  2335.     *
  2336.     * "It is legal to declare an array without a size and then
  2337.     *  later re-declare the same name as an array of the same
  2338.     *  type and specify a size."
  2339.     */
  2340.    if ((earlier->type->array_size() == 0)
  2341.        && var->type->is_array()
  2342.        && (var->type->element_type() == earlier->type->element_type())) {
  2343.       /* FINISHME: This doesn't match the qualifiers on the two
  2344.        * FINISHME: declarations.  It's not 100% clear whether this is
  2345.        * FINISHME: required or not.
  2346.        */
  2347.  
  2348.       const unsigned size = unsigned(var->type->array_size());
  2349.       check_builtin_array_max_size(var->name, size, loc, state);
  2350.       if ((size > 0) && (size <= earlier->max_array_access)) {
  2351.          _mesa_glsl_error(& loc, state, "array size must be > %u due to "
  2352.                           "previous access",
  2353.                           earlier->max_array_access);
  2354.       }
  2355.  
  2356.       earlier->type = var->type;
  2357.       delete var;
  2358.       var = NULL;
  2359.    } else if (state->ARB_fragment_coord_conventions_enable
  2360.               && strcmp(var->name, "gl_FragCoord") == 0
  2361.               && earlier->type == var->type
  2362.               && earlier->mode == var->mode) {
  2363.       /* Allow redeclaration of gl_FragCoord for ARB_fcc layout
  2364.        * qualifiers.
  2365.        */
  2366.       earlier->origin_upper_left = var->origin_upper_left;
  2367.       earlier->pixel_center_integer = var->pixel_center_integer;
  2368.  
  2369.       /* According to section 4.3.7 of the GLSL 1.30 spec,
  2370.        * the following built-in varaibles can be redeclared with an
  2371.        * interpolation qualifier:
  2372.        *    * gl_FrontColor
  2373.        *    * gl_BackColor
  2374.        *    * gl_FrontSecondaryColor
  2375.        *    * gl_BackSecondaryColor
  2376.        *    * gl_Color
  2377.        *    * gl_SecondaryColor
  2378.        */
  2379.    } else if (state->is_version(130, 0)
  2380.               && (strcmp(var->name, "gl_FrontColor") == 0
  2381.                   || strcmp(var->name, "gl_BackColor") == 0
  2382.                   || strcmp(var->name, "gl_FrontSecondaryColor") == 0
  2383.                   || strcmp(var->name, "gl_BackSecondaryColor") == 0
  2384.                   || strcmp(var->name, "gl_Color") == 0
  2385.                   || strcmp(var->name, "gl_SecondaryColor") == 0)
  2386.               && earlier->type == var->type
  2387.               && earlier->mode == var->mode) {
  2388.       earlier->interpolation = var->interpolation;
  2389.  
  2390.       /* Layout qualifiers for gl_FragDepth. */
  2391.    } else if ((state->AMD_conservative_depth_enable ||
  2392.                state->ARB_conservative_depth_enable)
  2393.               && strcmp(var->name, "gl_FragDepth") == 0
  2394.               && earlier->type == var->type
  2395.               && earlier->mode == var->mode) {
  2396.  
  2397.       /** From the AMD_conservative_depth spec:
  2398.        *     Within any shader, the first redeclarations of gl_FragDepth
  2399.        *     must appear before any use of gl_FragDepth.
  2400.        */
  2401.       if (earlier->used) {
  2402.          _mesa_glsl_error(&loc, state,
  2403.                           "the first redeclaration of gl_FragDepth "
  2404.                           "must appear before any use of gl_FragDepth");
  2405.       }
  2406.  
  2407.       /* Prevent inconsistent redeclaration of depth layout qualifier. */
  2408.       if (earlier->depth_layout != ir_depth_layout_none
  2409.           && earlier->depth_layout != var->depth_layout) {
  2410.          _mesa_glsl_error(&loc, state,
  2411.                           "gl_FragDepth: depth layout is declared here "
  2412.                           "as '%s, but it was previously declared as "
  2413.                           "'%s'",
  2414.                           depth_layout_string(var->depth_layout),
  2415.                           depth_layout_string(earlier->depth_layout));
  2416.       }
  2417.  
  2418.       earlier->depth_layout = var->depth_layout;
  2419.  
  2420.    } else {
  2421.       _mesa_glsl_error(&loc, state, "`%s' redeclared", decl->identifier);
  2422.    }
  2423.  
  2424.    return earlier;
  2425. }
  2426.  
  2427. /**
  2428.  * Generate the IR for an initializer in a variable declaration
  2429.  */
  2430. ir_rvalue *
  2431. process_initializer(ir_variable *var, ast_declaration *decl,
  2432.                     ast_fully_specified_type *type,
  2433.                     exec_list *initializer_instructions,
  2434.                     struct _mesa_glsl_parse_state *state)
  2435. {
  2436.    ir_rvalue *result = NULL;
  2437.  
  2438.    YYLTYPE initializer_loc = decl->initializer->get_location();
  2439.  
  2440.    /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec:
  2441.     *
  2442.     *    "All uniform variables are read-only and are initialized either
  2443.     *    directly by an application via API commands, or indirectly by
  2444.     *    OpenGL."
  2445.     */
  2446.    if (var->mode == ir_var_uniform) {
  2447.       state->check_version(120, 0, &initializer_loc,
  2448.                            "cannot initialize uniforms");
  2449.    }
  2450.  
  2451.    if (var->type->is_sampler()) {
  2452.       _mesa_glsl_error(& initializer_loc, state,
  2453.                        "cannot initialize samplers");
  2454.    }
  2455.  
  2456.    if ((var->mode == ir_var_shader_in) && (state->current_function == NULL)) {
  2457.       _mesa_glsl_error(& initializer_loc, state,
  2458.                        "cannot initialize %s shader input / %s",
  2459.                        _mesa_glsl_shader_target_name(state->target),
  2460.                        (state->target == vertex_shader)
  2461.                        ? "attribute" : "varying");
  2462.    }
  2463.  
  2464.    ir_dereference *const lhs = new(state) ir_dereference_variable(var);
  2465.    ir_rvalue *rhs = decl->initializer->hir(initializer_instructions,
  2466.                                            state);
  2467.  
  2468.    /* Calculate the constant value if this is a const or uniform
  2469.     * declaration.
  2470.     */
  2471.    if (type->qualifier.flags.q.constant
  2472.        || type->qualifier.flags.q.uniform) {
  2473.       ir_rvalue *new_rhs = validate_assignment(state, var->type, rhs, true);
  2474.       if (new_rhs != NULL) {
  2475.          rhs = new_rhs;
  2476.  
  2477.          ir_constant *constant_value = rhs->constant_expression_value();
  2478.          if (!constant_value) {
  2479.             /* If ARB_shading_language_420pack is enabled, initializers of
  2480.              * const-qualified local variables do not have to be constant
  2481.              * expressions. Const-qualified global variables must still be
  2482.              * initialized with constant expressions.
  2483.              */
  2484.             if (!state->ARB_shading_language_420pack_enable
  2485.                 || state->current_function == NULL) {
  2486.                _mesa_glsl_error(& initializer_loc, state,
  2487.                                 "initializer of %s variable `%s' must be a "
  2488.                                 "constant expression",
  2489.                                 (type->qualifier.flags.q.constant)
  2490.                                 ? "const" : "uniform",
  2491.                                 decl->identifier);
  2492.                if (var->type->is_numeric()) {
  2493.                   /* Reduce cascading errors. */
  2494.                   var->constant_value = ir_constant::zero(state, var->type);
  2495.                }
  2496.             }
  2497.          } else {
  2498.             rhs = constant_value;
  2499.             var->constant_value = constant_value;
  2500.          }
  2501.       } else {
  2502.          _mesa_glsl_error(&initializer_loc, state,
  2503.                           "initializer of type %s cannot be assigned to "
  2504.                           "variable of type %s",
  2505.                           rhs->type->name, var->type->name);
  2506.          if (var->type->is_numeric()) {
  2507.             /* Reduce cascading errors. */
  2508.             var->constant_value = ir_constant::zero(state, var->type);
  2509.          }
  2510.       }
  2511.    }
  2512.  
  2513.    if (rhs && !rhs->type->is_error()) {
  2514.       bool temp = var->read_only;
  2515.       if (type->qualifier.flags.q.constant)
  2516.          var->read_only = false;
  2517.  
  2518.       /* Never emit code to initialize a uniform.
  2519.        */
  2520.       const glsl_type *initializer_type;
  2521.       if (!type->qualifier.flags.q.uniform) {
  2522.          result = do_assignment(initializer_instructions, state,
  2523.                                 NULL,
  2524.                                 lhs, rhs, true,
  2525.                                 type->get_location());
  2526.          initializer_type = result->type;
  2527.       } else
  2528.          initializer_type = rhs->type;
  2529.  
  2530.       var->constant_initializer = rhs->constant_expression_value();
  2531.       var->has_initializer = true;
  2532.  
  2533.       /* If the declared variable is an unsized array, it must inherrit
  2534.        * its full type from the initializer.  A declaration such as
  2535.        *
  2536.        *     uniform float a[] = float[](1.0, 2.0, 3.0, 3.0);
  2537.        *
  2538.        * becomes
  2539.        *
  2540.        *     uniform float a[4] = float[](1.0, 2.0, 3.0, 3.0);
  2541.        *
  2542.        * The assignment generated in the if-statement (below) will also
  2543.        * automatically handle this case for non-uniforms.
  2544.        *
  2545.        * If the declared variable is not an array, the types must
  2546.        * already match exactly.  As a result, the type assignment
  2547.        * here can be done unconditionally.  For non-uniforms the call
  2548.        * to do_assignment can change the type of the initializer (via
  2549.        * the implicit conversion rules).  For uniforms the initializer
  2550.        * must be a constant expression, and the type of that expression
  2551.        * was validated above.
  2552.        */
  2553.       var->type = initializer_type;
  2554.  
  2555.       var->read_only = temp;
  2556.    }
  2557.  
  2558.    return result;
  2559. }
  2560.  
  2561. ir_rvalue *
  2562. ast_declarator_list::hir(exec_list *instructions,
  2563.                          struct _mesa_glsl_parse_state *state)
  2564. {
  2565.    void *ctx = state;
  2566.    const struct glsl_type *decl_type;
  2567.    const char *type_name = NULL;
  2568.    ir_rvalue *result = NULL;
  2569.    YYLTYPE loc = this->get_location();
  2570.  
  2571.    /* From page 46 (page 52 of the PDF) of the GLSL 1.50 spec:
  2572.     *
  2573.     *     "To ensure that a particular output variable is invariant, it is
  2574.     *     necessary to use the invariant qualifier. It can either be used to
  2575.     *     qualify a previously declared variable as being invariant
  2576.     *
  2577.     *         invariant gl_Position; // make existing gl_Position be invariant"
  2578.     *
  2579.     * In these cases the parser will set the 'invariant' flag in the declarator
  2580.     * list, and the type will be NULL.
  2581.     */
  2582.    if (this->invariant) {
  2583.       assert(this->type == NULL);
  2584.  
  2585.       if (state->current_function != NULL) {
  2586.          _mesa_glsl_error(& loc, state,
  2587.                           "All uses of `invariant' keyword must be at global "
  2588.                           "scope\n");
  2589.       }
  2590.  
  2591.       foreach_list_typed (ast_declaration, decl, link, &this->declarations) {
  2592.          assert(!decl->is_array);
  2593.          assert(decl->array_size == NULL);
  2594.          assert(decl->initializer == NULL);
  2595.  
  2596.          ir_variable *const earlier =
  2597.             state->symbols->get_variable(decl->identifier);
  2598.          if (earlier == NULL) {
  2599.             _mesa_glsl_error(& loc, state,
  2600.                              "Undeclared variable `%s' cannot be marked "
  2601.                              "invariant\n", decl->identifier);
  2602.          } else if ((state->target == vertex_shader)
  2603.                && (earlier->mode != ir_var_shader_out)) {
  2604.             _mesa_glsl_error(& loc, state,
  2605.                              "`%s' cannot be marked invariant, vertex shader "
  2606.                              "outputs only\n", decl->identifier);
  2607.          } else if ((state->target == fragment_shader)
  2608.                && (earlier->mode != ir_var_shader_in)) {
  2609.             _mesa_glsl_error(& loc, state,
  2610.                              "`%s' cannot be marked invariant, fragment shader "
  2611.                              "inputs only\n", decl->identifier);
  2612.          } else if (earlier->used) {
  2613.             _mesa_glsl_error(& loc, state,
  2614.                              "variable `%s' may not be redeclared "
  2615.                              "`invariant' after being used",
  2616.                              earlier->name);
  2617.          } else {
  2618.             earlier->invariant = true;
  2619.          }
  2620.       }
  2621.  
  2622.       /* Invariant redeclarations do not have r-values.
  2623.        */
  2624.       return NULL;
  2625.    }
  2626.  
  2627.    assert(this->type != NULL);
  2628.    assert(!this->invariant);
  2629.  
  2630.    /* The type specifier may contain a structure definition.  Process that
  2631.     * before any of the variable declarations.
  2632.     */
  2633.    (void) this->type->specifier->hir(instructions, state);
  2634.  
  2635.    decl_type = this->type->glsl_type(& type_name, state);
  2636.    if (this->declarations.is_empty()) {
  2637.       /* If there is no structure involved in the program text, there are two
  2638.        * possible scenarios:
  2639.        *
  2640.        * - The program text contained something like 'vec4;'.  This is an
  2641.        *   empty declaration.  It is valid but weird.  Emit a warning.
  2642.        *
  2643.        * - The program text contained something like 'S;' and 'S' is not the
  2644.        *   name of a known structure type.  This is both invalid and weird.
  2645.        *   Emit an error.
  2646.        *
  2647.        * - The program text contained something like 'mediump float;'
  2648.        *   when the programmer probably meant 'precision mediump
  2649.        *   float;' Emit a warning with a description of what they
  2650.        *   probably meant to do.
  2651.        *
  2652.        * Note that if decl_type is NULL and there is a structure involved,
  2653.        * there must have been some sort of error with the structure.  In this
  2654.        * case we assume that an error was already generated on this line of
  2655.        * code for the structure.  There is no need to generate an additional,
  2656.        * confusing error.
  2657.        */
  2658.       assert(this->type->specifier->structure == NULL || decl_type != NULL
  2659.              || state->error);
  2660.  
  2661.       if (decl_type == NULL) {
  2662.          _mesa_glsl_error(&loc, state,
  2663.                           "invalid type `%s' in empty declaration",
  2664.                           type_name);
  2665.       } else if (this->type->qualifier.precision != ast_precision_none) {
  2666.          if (this->type->specifier->structure != NULL) {
  2667.             _mesa_glsl_error(&loc, state,
  2668.                              "precision qualifiers can't be applied "
  2669.                              "to structures");
  2670.          } else {
  2671.             static const char *const precision_names[] = {
  2672.                "highp",
  2673.                "highp",
  2674.                "mediump",
  2675.                "lowp"
  2676.             };
  2677.  
  2678.             _mesa_glsl_warning(&loc, state,
  2679.                                "empty declaration with precision qualifier, "
  2680.                                "to set the default precision, use "
  2681.                                "`precision %s %s;'",
  2682.                                precision_names[this->type->qualifier.precision],
  2683.                                type_name);
  2684.          }
  2685.       } else {
  2686.          _mesa_glsl_warning(&loc, state, "empty declaration");
  2687.       }
  2688.    }
  2689.  
  2690.    foreach_list_typed (ast_declaration, decl, link, &this->declarations) {
  2691.       const struct glsl_type *var_type;
  2692.       ir_variable *var;
  2693.  
  2694.       /* FINISHME: Emit a warning if a variable declaration shadows a
  2695.        * FINISHME: declaration at a higher scope.
  2696.        */
  2697.  
  2698.       if ((decl_type == NULL) || decl_type->is_void()) {
  2699.          if (type_name != NULL) {
  2700.             _mesa_glsl_error(& loc, state,
  2701.                              "invalid type `%s' in declaration of `%s'",
  2702.                              type_name, decl->identifier);
  2703.          } else {
  2704.             _mesa_glsl_error(& loc, state,
  2705.                              "invalid type in declaration of `%s'",
  2706.                              decl->identifier);
  2707.          }
  2708.          continue;
  2709.       }
  2710.  
  2711.       if (decl->is_array) {
  2712.          var_type = process_array_type(&loc, decl_type, decl->array_size,
  2713.                                        state);
  2714.          if (var_type->is_error())
  2715.             continue;
  2716.       } else {
  2717.          var_type = decl_type;
  2718.       }
  2719.  
  2720.       var = new(ctx) ir_variable(var_type, decl->identifier, ir_var_auto);
  2721.  
  2722.       /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification;
  2723.        *
  2724.        *     "Global variables can only use the qualifiers const,
  2725.        *     attribute, uni form, or varying. Only one may be
  2726.        *     specified.
  2727.        *
  2728.        *     Local variables can only use the qualifier const."
  2729.        *
  2730.        * This is relaxed in GLSL 1.30 and GLSL ES 3.00.  It is also relaxed by
  2731.        * any extension that adds the 'layout' keyword.
  2732.        */
  2733.       if (!state->is_version(130, 300)
  2734.           && !state->ARB_explicit_attrib_location_enable
  2735.           && !state->ARB_fragment_coord_conventions_enable) {
  2736.          if (this->type->qualifier.flags.q.out) {
  2737.             _mesa_glsl_error(& loc, state,
  2738.                              "`out' qualifier in declaration of `%s' "
  2739.                              "only valid for function parameters in %s.",
  2740.                              decl->identifier, state->get_version_string());
  2741.          }
  2742.          if (this->type->qualifier.flags.q.in) {
  2743.             _mesa_glsl_error(& loc, state,
  2744.                              "`in' qualifier in declaration of `%s' "
  2745.                              "only valid for function parameters in %s.",
  2746.                              decl->identifier, state->get_version_string());
  2747.          }
  2748.          /* FINISHME: Test for other invalid qualifiers. */
  2749.       }
  2750.  
  2751.       apply_type_qualifier_to_variable(& this->type->qualifier, var, state,
  2752.                                        & loc, false);
  2753.  
  2754.       if (this->type->qualifier.flags.q.invariant) {
  2755.          if ((state->target == vertex_shader) &&
  2756.              var->mode != ir_var_shader_out) {
  2757.             _mesa_glsl_error(& loc, state,
  2758.                              "`%s' cannot be marked invariant, vertex shader "
  2759.                              "outputs only\n", var->name);
  2760.          } else if ((state->target == fragment_shader) &&
  2761.                     var->mode != ir_var_shader_in) {
  2762.             /* FINISHME: Note that this doesn't work for invariant on
  2763.              * a function signature inval
  2764.              */
  2765.             _mesa_glsl_error(& loc, state,
  2766.                              "`%s' cannot be marked invariant, fragment shader "
  2767.                              "inputs only\n", var->name);
  2768.          }
  2769.       }
  2770.  
  2771.       if (state->current_function != NULL) {
  2772.          const char *mode = NULL;
  2773.          const char *extra = "";
  2774.  
  2775.          /* There is no need to check for 'inout' here because the parser will
  2776.           * only allow that in function parameter lists.
  2777.           */
  2778.          if (this->type->qualifier.flags.q.attribute) {
  2779.             mode = "attribute";
  2780.          } else if (this->type->qualifier.flags.q.uniform) {
  2781.             mode = "uniform";
  2782.          } else if (this->type->qualifier.flags.q.varying) {
  2783.             mode = "varying";
  2784.          } else if (this->type->qualifier.flags.q.in) {
  2785.             mode = "in";
  2786.             extra = " or in function parameter list";
  2787.          } else if (this->type->qualifier.flags.q.out) {
  2788.             mode = "out";
  2789.             extra = " or in function parameter list";
  2790.          }
  2791.  
  2792.          if (mode) {
  2793.             _mesa_glsl_error(& loc, state,
  2794.                              "%s variable `%s' must be declared at "
  2795.                              "global scope%s",
  2796.                              mode, var->name, extra);
  2797.          }
  2798.       } else if (var->mode == ir_var_shader_in) {
  2799.          var->read_only = true;
  2800.  
  2801.          if (state->target == vertex_shader) {
  2802.             bool error_emitted = false;
  2803.  
  2804.             /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
  2805.              *
  2806.              *    "Vertex shader inputs can only be float, floating-point
  2807.              *    vectors, matrices, signed and unsigned integers and integer
  2808.              *    vectors. Vertex shader inputs can also form arrays of these
  2809.              *    types, but not structures."
  2810.              *
  2811.              * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec:
  2812.              *
  2813.              *    "Vertex shader inputs can only be float, floating-point
  2814.              *    vectors, matrices, signed and unsigned integers and integer
  2815.              *    vectors. They cannot be arrays or structures."
  2816.              *
  2817.              * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec:
  2818.              *
  2819.              *    "The attribute qualifier can be used only with float,
  2820.              *    floating-point vectors, and matrices. Attribute variables
  2821.              *    cannot be declared as arrays or structures."
  2822.              *
  2823.              * From page 33 (page 39 of the PDF) of the GLSL ES 3.00 spec:
  2824.              *
  2825.              *    "Vertex shader inputs can only be float, floating-point
  2826.              *    vectors, matrices, signed and unsigned integers and integer
  2827.              *    vectors. Vertex shader inputs cannot be arrays or
  2828.              *    structures."
  2829.              */
  2830.             const glsl_type *check_type = var->type->is_array()
  2831.                ? var->type->fields.array : var->type;
  2832.  
  2833.             switch (check_type->base_type) {
  2834.             case GLSL_TYPE_FLOAT:
  2835.                break;
  2836.             case GLSL_TYPE_UINT:
  2837.             case GLSL_TYPE_INT:
  2838.                if (state->is_version(120, 300))
  2839.                   break;
  2840.                /* FALLTHROUGH */
  2841.             default:
  2842.                _mesa_glsl_error(& loc, state,
  2843.                                 "vertex shader input / attribute cannot have "
  2844.                                 "type %s`%s'",
  2845.                                 var->type->is_array() ? "array of " : "",
  2846.                                 check_type->name);
  2847.                error_emitted = true;
  2848.             }
  2849.  
  2850.             if (!error_emitted && var->type->is_array() &&
  2851.                 !state->check_version(150, 0, &loc,
  2852.                                       "vertex shader input / attribute "
  2853.                                       "cannot have array type")) {
  2854.                error_emitted = true;
  2855.             }
  2856.          }
  2857.       }
  2858.  
  2859.       /* Integer fragment inputs must be qualified with 'flat'.  In GLSL ES,
  2860.        * so must integer vertex outputs.
  2861.        *
  2862.        * From section 4.3.4 ("Inputs") of the GLSL 1.50 spec:
  2863.        *    "Fragment shader inputs that are signed or unsigned integers or
  2864.        *    integer vectors must be qualified with the interpolation qualifier
  2865.        *    flat."
  2866.        *
  2867.        * From section 4.3.4 ("Input Variables") of the GLSL 3.00 ES spec:
  2868.        *    "Fragment shader inputs that are, or contain, signed or unsigned
  2869.        *    integers or integer vectors must be qualified with the
  2870.        *    interpolation qualifier flat."
  2871.        *
  2872.        * From section 4.3.6 ("Output Variables") of the GLSL 3.00 ES spec:
  2873.        *    "Vertex shader outputs that are, or contain, signed or unsigned
  2874.        *    integers or integer vectors must be qualified with the
  2875.        *    interpolation qualifier flat."
  2876.        *
  2877.        * Note that prior to GLSL 1.50, this requirement applied to vertex
  2878.        * outputs rather than fragment inputs.  That creates problems in the
  2879.        * presence of geometry shaders, so we adopt the GLSL 1.50 rule for all
  2880.        * desktop GL shaders.  For GLSL ES shaders, we follow the spec and
  2881.        * apply the restriction to both vertex outputs and fragment inputs.
  2882.        *
  2883.        * Note also that the desktop GLSL specs are missing the text "or
  2884.        * contain"; this is presumably an oversight, since there is no
  2885.        * reasonable way to interpolate a fragment shader input that contains
  2886.        * an integer.
  2887.        */
  2888.       if (state->is_version(130, 300) &&
  2889.           var->type->contains_integer() &&
  2890.           var->interpolation != INTERP_QUALIFIER_FLAT &&
  2891.           ((state->target == fragment_shader && var->mode == ir_var_shader_in)
  2892.            || (state->target == vertex_shader && var->mode == ir_var_shader_out
  2893.                && state->es_shader))) {
  2894.          const char *var_type = (state->target == vertex_shader) ?
  2895.             "vertex output" : "fragment input";
  2896.          _mesa_glsl_error(&loc, state, "If a %s is (or contains) "
  2897.                           "an integer, then it must be qualified with 'flat'",
  2898.                           var_type);
  2899.       }
  2900.  
  2901.  
  2902.       /* Interpolation qualifiers cannot be applied to 'centroid' and
  2903.        * 'centroid varying'.
  2904.        *
  2905.        * From page 29 (page 35 of the PDF) of the GLSL 1.30 spec:
  2906.        *    "interpolation qualifiers may only precede the qualifiers in,
  2907.        *    centroid in, out, or centroid out in a declaration. They do not apply
  2908.        *    to the deprecated storage qualifiers varying or centroid varying."
  2909.        *
  2910.        * These deprecated storage qualifiers do not exist in GLSL ES 3.00.
  2911.        */
  2912.       if (state->is_version(130, 0)
  2913.           && this->type->qualifier.has_interpolation()
  2914.           && this->type->qualifier.flags.q.varying) {
  2915.  
  2916.          const char *i = this->type->qualifier.interpolation_string();
  2917.          assert(i != NULL);
  2918.          const char *s;
  2919.          if (this->type->qualifier.flags.q.centroid)
  2920.             s = "centroid varying";
  2921.          else
  2922.             s = "varying";
  2923.  
  2924.          _mesa_glsl_error(&loc, state,
  2925.                           "qualifier '%s' cannot be applied to the "
  2926.                           "deprecated storage qualifier '%s'", i, s);
  2927.       }
  2928.  
  2929.  
  2930.       /* Interpolation qualifiers can only apply to vertex shader outputs and
  2931.        * fragment shader inputs.
  2932.        *
  2933.        * From page 29 (page 35 of the PDF) of the GLSL 1.30 spec:
  2934.        *    "Outputs from a vertex shader (out) and inputs to a fragment
  2935.        *    shader (in) can be further qualified with one or more of these
  2936.        *    interpolation qualifiers"
  2937.        *
  2938.        * From page 31 (page 37 of the PDF) of the GLSL ES 3.00 spec:
  2939.        *    "These interpolation qualifiers may only precede the qualifiers
  2940.        *    in, centroid in, out, or centroid out in a declaration. They do
  2941.        *    not apply to inputs into a vertex shader or outputs from a
  2942.        *    fragment shader."
  2943.        */
  2944.       if (state->is_version(130, 300)
  2945.           && this->type->qualifier.has_interpolation()) {
  2946.  
  2947.          const char *i = this->type->qualifier.interpolation_string();
  2948.          assert(i != NULL);
  2949.  
  2950.          switch (state->target) {
  2951.          case vertex_shader:
  2952.             if (this->type->qualifier.flags.q.in) {
  2953.                _mesa_glsl_error(&loc, state,
  2954.                                 "qualifier '%s' cannot be applied to vertex "
  2955.                                 "shader inputs", i);
  2956.             }
  2957.             break;
  2958.          case fragment_shader:
  2959.             if (this->type->qualifier.flags.q.out) {
  2960.                _mesa_glsl_error(&loc, state,
  2961.                                 "qualifier '%s' cannot be applied to fragment "
  2962.                                 "shader outputs", i);
  2963.             }
  2964.             break;
  2965.          default:
  2966.             assert(0);
  2967.          }
  2968.       }
  2969.  
  2970.  
  2971.       /* From section 4.3.4 of the GLSL 1.30 spec:
  2972.        *    "It is an error to use centroid in in a vertex shader."
  2973.        *
  2974.        * From section 4.3.4 of the GLSL ES 3.00 spec:
  2975.        *    "It is an error to use centroid in or interpolation qualifiers in
  2976.        *    a vertex shader input."
  2977.        */
  2978.       if (state->is_version(130, 300)
  2979.           && this->type->qualifier.flags.q.centroid
  2980.           && this->type->qualifier.flags.q.in
  2981.           && state->target == vertex_shader) {
  2982.  
  2983.          _mesa_glsl_error(&loc, state,
  2984.                           "'centroid in' cannot be used in a vertex shader");
  2985.       }
  2986.  
  2987.       /* Section 4.3.6 of the GLSL 1.30 specification states:
  2988.        * "It is an error to use centroid out in a fragment shader."
  2989.        *
  2990.        * The GL_ARB_shading_language_420pack extension specification states:
  2991.        * "It is an error to use auxiliary storage qualifiers or interpolation
  2992.        *  qualifiers on an output in a fragment shader."
  2993.        */
  2994.       if (state->target == fragment_shader &&
  2995.           this->type->qualifier.flags.q.out &&
  2996.           this->type->qualifier.has_auxiliary_storage()) {
  2997.          _mesa_glsl_error(&loc, state,
  2998.                           "auxiliary storage qualifiers cannot be used on "
  2999.                           "fragment shader outputs");
  3000.       }
  3001.  
  3002.       /* Precision qualifiers exists only in GLSL versions 1.00 and >= 1.30.
  3003.        */
  3004.       if (this->type->qualifier.precision != ast_precision_none) {
  3005.          state->check_precision_qualifiers_allowed(&loc);
  3006.       }
  3007.  
  3008.  
  3009.       /* Precision qualifiers apply to floating point, integer and sampler
  3010.        * types.
  3011.        *
  3012.        * Section 4.5.2 (Precision Qualifiers) of the GLSL 1.30 spec says:
  3013.        *    "Any floating point or any integer declaration can have the type
  3014.        *    preceded by one of these precision qualifiers [...] Literal
  3015.        *    constants do not have precision qualifiers. Neither do Boolean
  3016.        *    variables.
  3017.        *
  3018.        * Section 4.5 (Precision and Precision Qualifiers) of the GLSL 1.30
  3019.        * spec also says:
  3020.        *
  3021.        *     "Precision qualifiers are added for code portability with OpenGL
  3022.        *     ES, not for functionality. They have the same syntax as in OpenGL
  3023.        *     ES."
  3024.        *
  3025.        * Section 8 (Built-In Functions) of the GLSL ES 1.00 spec says:
  3026.        *
  3027.        *     "uniform lowp sampler2D sampler;
  3028.        *     highp vec2 coord;
  3029.        *     ...
  3030.        *     lowp vec4 col = texture2D (sampler, coord);
  3031.        *                                            // texture2D returns lowp"
  3032.        *
  3033.        * From this, we infer that GLSL 1.30 (and later) should allow precision
  3034.        * qualifiers on sampler types just like float and integer types.
  3035.        */
  3036.       if (this->type->qualifier.precision != ast_precision_none
  3037.           && !var->type->is_float()
  3038.           && !var->type->is_integer()
  3039.           && !var->type->is_record()
  3040.           && !var->type->is_sampler()
  3041.           && !(var->type->is_array()
  3042.                && (var->type->fields.array->is_float()
  3043.                    || var->type->fields.array->is_integer()))) {
  3044.  
  3045.          _mesa_glsl_error(&loc, state,
  3046.                           "precision qualifiers apply only to floating point"
  3047.                           ", integer and sampler types");
  3048.       }
  3049.  
  3050.       /* From page 17 (page 23 of the PDF) of the GLSL 1.20 spec:
  3051.        *
  3052.        *    "[Sampler types] can only be declared as function
  3053.        *    parameters or uniform variables (see Section 4.3.5
  3054.        *    "Uniform")".
  3055.        */
  3056.       if (var_type->contains_sampler() &&
  3057.           !this->type->qualifier.flags.q.uniform) {
  3058.          _mesa_glsl_error(&loc, state, "samplers must be declared uniform");
  3059.       }
  3060.  
  3061.       /* Process the initializer and add its instructions to a temporary
  3062.        * list.  This list will be added to the instruction stream (below) after
  3063.        * the declaration is added.  This is done because in some cases (such as
  3064.        * redeclarations) the declaration may not actually be added to the
  3065.        * instruction stream.
  3066.        */
  3067.       exec_list initializer_instructions;
  3068.       ir_variable *earlier = get_variable_being_redeclared(var, decl, state);
  3069.  
  3070.       if (decl->initializer != NULL) {
  3071.          result = process_initializer((earlier == NULL) ? var : earlier,
  3072.                                       decl, this->type,
  3073.                                       &initializer_instructions, state);
  3074.       }
  3075.  
  3076.       /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec:
  3077.        *
  3078.        *     "It is an error to write to a const variable outside of
  3079.        *      its declaration, so they must be initialized when
  3080.        *      declared."
  3081.        */
  3082.       if (this->type->qualifier.flags.q.constant && decl->initializer == NULL) {
  3083.          _mesa_glsl_error(& loc, state,
  3084.                           "const declaration of `%s' must be initialized",
  3085.                           decl->identifier);
  3086.       }
  3087.  
  3088.       if (state->es_shader) {
  3089.          const glsl_type *const t = (earlier == NULL)
  3090.             ? var->type : earlier->type;
  3091.  
  3092.          if (t->is_array() && t->length == 0)
  3093.             /* Section 10.17 of the GLSL ES 1.00 specification states that
  3094.              * unsized array declarations have been removed from the language.
  3095.              * Arrays that are sized using an initializer are still explicitly
  3096.              * sized.  However, GLSL ES 1.00 does not allow array
  3097.              * initializers.  That is only allowed in GLSL ES 3.00.
  3098.              *
  3099.              * Section 4.1.9 (Arrays) of the GLSL ES 3.00 spec says:
  3100.              *
  3101.              *     "An array type can also be formed without specifying a size
  3102.              *     if the definition includes an initializer:
  3103.              *
  3104.              *         float x[] = float[2] (1.0, 2.0);     // declares an array of size 2
  3105.              *         float y[] = float[] (1.0, 2.0, 3.0); // declares an array of size 3
  3106.              *
  3107.              *         float a[5];
  3108.              *         float b[] = a;"
  3109.              */
  3110.             _mesa_glsl_error(& loc, state,
  3111.                              "unsized array declarations are not allowed in "
  3112.                              "GLSL ES");
  3113.       }
  3114.  
  3115.       /* If the declaration is not a redeclaration, there are a few additional
  3116.        * semantic checks that must be applied.  In addition, variable that was
  3117.        * created for the declaration should be added to the IR stream.
  3118.        */
  3119.       if (earlier == NULL) {
  3120.          /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
  3121.           *
  3122.           *   "Identifiers starting with "gl_" are reserved for use by
  3123.           *   OpenGL, and may not be declared in a shader as either a
  3124.           *   variable or a function."
  3125.           */
  3126.          if (strncmp(decl->identifier, "gl_", 3) == 0)
  3127.             _mesa_glsl_error(& loc, state,
  3128.                              "identifier `%s' uses reserved `gl_' prefix",
  3129.                              decl->identifier);
  3130.          else if (strstr(decl->identifier, "__")) {
  3131.             /* From page 14 (page 20 of the PDF) of the GLSL 1.10
  3132.              * spec:
  3133.              *
  3134.              *     "In addition, all identifiers containing two
  3135.              *      consecutive underscores (__) are reserved as
  3136.              *      possible future keywords."
  3137.              */
  3138.             _mesa_glsl_error(& loc, state,
  3139.                              "identifier `%s' uses reserved `__' string",
  3140.                              decl->identifier);
  3141.          }
  3142.  
  3143.          /* Add the variable to the symbol table.  Note that the initializer's
  3144.           * IR was already processed earlier (though it hasn't been emitted
  3145.           * yet), without the variable in scope.
  3146.           *
  3147.           * This differs from most C-like languages, but it follows the GLSL
  3148.           * specification.  From page 28 (page 34 of the PDF) of the GLSL 1.50
  3149.           * spec:
  3150.           *
  3151.           *     "Within a declaration, the scope of a name starts immediately
  3152.           *     after the initializer if present or immediately after the name
  3153.           *     being declared if not."
  3154.           */
  3155.          if (!state->symbols->add_variable(var)) {
  3156.             YYLTYPE loc = this->get_location();
  3157.             _mesa_glsl_error(&loc, state, "name `%s' already taken in the "
  3158.                              "current scope", decl->identifier);
  3159.             continue;
  3160.          }
  3161.  
  3162.          /* Push the variable declaration to the top.  It means that all the
  3163.           * variable declarations will appear in a funny last-to-first order,
  3164.           * but otherwise we run into trouble if a function is prototyped, a
  3165.           * global var is decled, then the function is defined with usage of
  3166.           * the global var.  See glslparsertest's CorrectModule.frag.
  3167.           */
  3168.          instructions->push_head(var);
  3169.       }
  3170.  
  3171.       instructions->append_list(&initializer_instructions);
  3172.    }
  3173.  
  3174.  
  3175.    /* Generally, variable declarations do not have r-values.  However,
  3176.     * one is used for the declaration in
  3177.     *
  3178.     * while (bool b = some_condition()) {
  3179.     *   ...
  3180.     * }
  3181.     *
  3182.     * so we return the rvalue from the last seen declaration here.
  3183.     */
  3184.    return result;
  3185. }
  3186.  
  3187.  
  3188. ir_rvalue *
  3189. ast_parameter_declarator::hir(exec_list *instructions,
  3190.                               struct _mesa_glsl_parse_state *state)
  3191. {
  3192.    void *ctx = state;
  3193.    const struct glsl_type *type;
  3194.    const char *name = NULL;
  3195.    YYLTYPE loc = this->get_location();
  3196.  
  3197.    type = this->type->glsl_type(& name, state);
  3198.  
  3199.    if (type == NULL) {
  3200.       if (name != NULL) {
  3201.          _mesa_glsl_error(& loc, state,
  3202.                           "invalid type `%s' in declaration of `%s'",
  3203.                           name, this->identifier);
  3204.       } else {
  3205.          _mesa_glsl_error(& loc, state,
  3206.                           "invalid type in declaration of `%s'",
  3207.                           this->identifier);
  3208.       }
  3209.  
  3210.       type = glsl_type::error_type;
  3211.    }
  3212.  
  3213.    /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec:
  3214.     *
  3215.     *    "Functions that accept no input arguments need not use void in the
  3216.     *    argument list because prototypes (or definitions) are required and
  3217.     *    therefore there is no ambiguity when an empty argument list "( )" is
  3218.     *    declared. The idiom "(void)" as a parameter list is provided for
  3219.     *    convenience."
  3220.     *
  3221.     * Placing this check here prevents a void parameter being set up
  3222.     * for a function, which avoids tripping up checks for main taking
  3223.     * parameters and lookups of an unnamed symbol.
  3224.     */
  3225.    if (type->is_void()) {
  3226.       if (this->identifier != NULL)
  3227.          _mesa_glsl_error(& loc, state,
  3228.                           "named parameter cannot have type `void'");
  3229.  
  3230.       is_void = true;
  3231.       return NULL;
  3232.    }
  3233.  
  3234.    if (formal_parameter && (this->identifier == NULL)) {
  3235.       _mesa_glsl_error(& loc, state, "formal parameter lacks a name");
  3236.       return NULL;
  3237.    }
  3238.  
  3239.    /* This only handles "vec4 foo[..]".  The earlier specifier->glsl_type(...)
  3240.     * call already handled the "vec4[..] foo" case.
  3241.     */
  3242.    if (this->is_array) {
  3243.       type = process_array_type(&loc, type, this->array_size, state);
  3244.    }
  3245.  
  3246.    if (!type->is_error() && type->array_size() == 0) {
  3247.       _mesa_glsl_error(&loc, state, "arrays passed as parameters must have "
  3248.                        "a declared size.");
  3249.       type = glsl_type::error_type;
  3250.    }
  3251.  
  3252.    is_void = false;
  3253.    ir_variable *var = new(ctx)
  3254.       ir_variable(type, this->identifier, ir_var_function_in);
  3255.  
  3256.    /* Apply any specified qualifiers to the parameter declaration.  Note that
  3257.     * for function parameters the default mode is 'in'.
  3258.     */
  3259.    apply_type_qualifier_to_variable(& this->type->qualifier, var, state, & loc,
  3260.                                     true);
  3261.  
  3262.    /* From page 17 (page 23 of the PDF) of the GLSL 1.20 spec:
  3263.     *
  3264.     *    "Samplers cannot be treated as l-values; hence cannot be used
  3265.     *    as out or inout function parameters, nor can they be assigned
  3266.     *    into."
  3267.     */
  3268.    if ((var->mode == ir_var_function_inout || var->mode == ir_var_function_out)
  3269.        && type->contains_sampler()) {
  3270.       _mesa_glsl_error(&loc, state, "out and inout parameters cannot contain samplers");
  3271.       type = glsl_type::error_type;
  3272.    }
  3273.  
  3274.    /* From page 39 (page 45 of the PDF) of the GLSL 1.10 spec:
  3275.     *
  3276.     *    "When calling a function, expressions that do not evaluate to
  3277.     *     l-values cannot be passed to parameters declared as out or inout."
  3278.     *
  3279.     * From page 32 (page 38 of the PDF) of the GLSL 1.10 spec:
  3280.     *
  3281.     *    "Other binary or unary expressions, non-dereferenced arrays,
  3282.     *     function names, swizzles with repeated fields, and constants
  3283.     *     cannot be l-values."
  3284.     *
  3285.     * So for GLSL 1.10, passing an array as an out or inout parameter is not
  3286.     * allowed.  This restriction is removed in GLSL 1.20, and in GLSL ES.
  3287.     */
  3288.    if ((var->mode == ir_var_function_inout || var->mode == ir_var_function_out)
  3289.        && type->is_array()
  3290.        && !state->check_version(120, 100, &loc,
  3291.                                 "Arrays cannot be out or inout parameters")) {
  3292.       type = glsl_type::error_type;
  3293.    }
  3294.  
  3295.    instructions->push_tail(var);
  3296.  
  3297.    /* Parameter declarations do not have r-values.
  3298.     */
  3299.    return NULL;
  3300. }
  3301.  
  3302.  
  3303. void
  3304. ast_parameter_declarator::parameters_to_hir(exec_list *ast_parameters,
  3305.                                             bool formal,
  3306.                                             exec_list *ir_parameters,
  3307.                                             _mesa_glsl_parse_state *state)
  3308. {
  3309.    ast_parameter_declarator *void_param = NULL;
  3310.    unsigned count = 0;
  3311.  
  3312.    foreach_list_typed (ast_parameter_declarator, param, link, ast_parameters) {
  3313.       param->formal_parameter = formal;
  3314.       param->hir(ir_parameters, state);
  3315.  
  3316.       if (param->is_void)
  3317.          void_param = param;
  3318.  
  3319.       count++;
  3320.    }
  3321.  
  3322.    if ((void_param != NULL) && (count > 1)) {
  3323.       YYLTYPE loc = void_param->get_location();
  3324.  
  3325.       _mesa_glsl_error(& loc, state,
  3326.                        "`void' parameter must be only parameter");
  3327.    }
  3328. }
  3329.  
  3330.  
  3331. void
  3332. emit_function(_mesa_glsl_parse_state *state, ir_function *f)
  3333. {
  3334.    /* IR invariants disallow function declarations or definitions
  3335.     * nested within other function definitions.  But there is no
  3336.     * requirement about the relative order of function declarations
  3337.     * and definitions with respect to one another.  So simply insert
  3338.     * the new ir_function block at the end of the toplevel instruction
  3339.     * list.
  3340.     */
  3341.    state->toplevel_ir->push_tail(f);
  3342. }
  3343.  
  3344.  
  3345. ir_rvalue *
  3346. ast_function::hir(exec_list *instructions,
  3347.                   struct _mesa_glsl_parse_state *state)
  3348. {
  3349.    void *ctx = state;
  3350.    ir_function *f = NULL;
  3351.    ir_function_signature *sig = NULL;
  3352.    exec_list hir_parameters;
  3353.  
  3354.    const char *const name = identifier;
  3355.  
  3356.    /* New functions are always added to the top-level IR instruction stream,
  3357.     * so this instruction list pointer is ignored.  See also emit_function
  3358.     * (called below).
  3359.     */
  3360.    (void) instructions;
  3361.  
  3362.    /* From page 21 (page 27 of the PDF) of the GLSL 1.20 spec,
  3363.     *
  3364.     *   "Function declarations (prototypes) cannot occur inside of functions;
  3365.     *   they must be at global scope, or for the built-in functions, outside
  3366.     *   the global scope."
  3367.     *
  3368.     * From page 27 (page 33 of the PDF) of the GLSL ES 1.00.16 spec,
  3369.     *
  3370.     *   "User defined functions may only be defined within the global scope."
  3371.     *
  3372.     * Note that this language does not appear in GLSL 1.10.
  3373.     */
  3374.    if ((state->current_function != NULL) &&
  3375.        state->is_version(120, 100)) {
  3376.       YYLTYPE loc = this->get_location();
  3377.       _mesa_glsl_error(&loc, state,
  3378.                        "declaration of function `%s' not allowed within "
  3379.                        "function body", name);
  3380.    }
  3381.  
  3382.    /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
  3383.     *
  3384.     *   "Identifiers starting with "gl_" are reserved for use by
  3385.     *   OpenGL, and may not be declared in a shader as either a
  3386.     *   variable or a function."
  3387.     */
  3388.    if (strncmp(name, "gl_", 3) == 0) {
  3389.       YYLTYPE loc = this->get_location();
  3390.       _mesa_glsl_error(&loc, state,
  3391.                        "identifier `%s' uses reserved `gl_' prefix", name);
  3392.    }
  3393.  
  3394.    /* Convert the list of function parameters to HIR now so that they can be
  3395.     * used below to compare this function's signature with previously seen
  3396.     * signatures for functions with the same name.
  3397.     */
  3398.    ast_parameter_declarator::parameters_to_hir(& this->parameters,
  3399.                                                is_definition,
  3400.                                                & hir_parameters, state);
  3401.  
  3402.    const char *return_type_name;
  3403.    const glsl_type *return_type =
  3404.       this->return_type->glsl_type(& return_type_name, state);
  3405.  
  3406.    if (!return_type) {
  3407.       YYLTYPE loc = this->get_location();
  3408.       _mesa_glsl_error(&loc, state,
  3409.                        "function `%s' has undeclared return type `%s'",
  3410.                        name, return_type_name);
  3411.       return_type = glsl_type::error_type;
  3412.    }
  3413.  
  3414.    /* From page 56 (page 62 of the PDF) of the GLSL 1.30 spec:
  3415.     * "No qualifier is allowed on the return type of a function."
  3416.     */
  3417.    if (this->return_type->has_qualifiers()) {
  3418.       YYLTYPE loc = this->get_location();
  3419.       _mesa_glsl_error(& loc, state,
  3420.                        "function `%s' return type has qualifiers", name);
  3421.    }
  3422.  
  3423.    /* Section 6.1 (Function Definitions) of the GLSL 1.20 spec says:
  3424.     *
  3425.     *     "Arrays are allowed as arguments and as the return type. In both
  3426.     *     cases, the array must be explicitly sized."
  3427.     */
  3428.    if (return_type->is_array() && return_type->length == 0) {
  3429.       YYLTYPE loc = this->get_location();
  3430.       _mesa_glsl_error(& loc, state,
  3431.                        "function `%s' return type array must be explicitly "
  3432.                        "sized", name);
  3433.    }
  3434.  
  3435.    /* From page 17 (page 23 of the PDF) of the GLSL 1.20 spec:
  3436.     *
  3437.     *    "[Sampler types] can only be declared as function parameters
  3438.     *    or uniform variables (see Section 4.3.5 "Uniform")".
  3439.     */
  3440.    if (return_type->contains_sampler()) {
  3441.       YYLTYPE loc = this->get_location();
  3442.       _mesa_glsl_error(&loc, state,
  3443.                        "function `%s' return type can't contain a sampler",
  3444.                        name);
  3445.    }
  3446.  
  3447.    /* Verify that this function's signature either doesn't match a previously
  3448.     * seen signature for a function with the same name, or, if a match is found,
  3449.     * that the previously seen signature does not have an associated definition.
  3450.     */
  3451.    f = state->symbols->get_function(name);
  3452.    if (f != NULL && (state->es_shader || f->has_user_signature())) {
  3453.       sig = f->exact_matching_signature(&hir_parameters);
  3454.       if (sig != NULL) {
  3455.          const char *badvar = sig->qualifiers_match(&hir_parameters);
  3456.          if (badvar != NULL) {
  3457.             YYLTYPE loc = this->get_location();
  3458.  
  3459.             _mesa_glsl_error(&loc, state, "function `%s' parameter `%s' "
  3460.                              "qualifiers don't match prototype", name, badvar);
  3461.          }
  3462.  
  3463.          if (sig->return_type != return_type) {
  3464.             YYLTYPE loc = this->get_location();
  3465.  
  3466.             _mesa_glsl_error(&loc, state, "function `%s' return type doesn't "
  3467.                              "match prototype", name);
  3468.          }
  3469.  
  3470.          if (sig->is_defined) {
  3471.             if (is_definition) {
  3472.                YYLTYPE loc = this->get_location();
  3473.                _mesa_glsl_error(& loc, state, "function `%s' redefined", name);
  3474.             } else {
  3475.                /* We just encountered a prototype that exactly matches a
  3476.                 * function that's already been defined.  This is redundant,
  3477.                 * and we should ignore it.
  3478.                 */
  3479.                return NULL;
  3480.             }
  3481.          }
  3482.       }
  3483.    } else {
  3484.       f = new(ctx) ir_function(name);
  3485.       if (!state->symbols->add_function(f)) {
  3486.          /* This function name shadows a non-function use of the same name. */
  3487.          YYLTYPE loc = this->get_location();
  3488.  
  3489.          _mesa_glsl_error(&loc, state, "function name `%s' conflicts with "
  3490.                           "non-function", name);
  3491.          return NULL;
  3492.       }
  3493.  
  3494.       emit_function(state, f);
  3495.    }
  3496.  
  3497.    /* Verify the return type of main() */
  3498.    if (strcmp(name, "main") == 0) {
  3499.       if (! return_type->is_void()) {
  3500.          YYLTYPE loc = this->get_location();
  3501.  
  3502.          _mesa_glsl_error(& loc, state, "main() must return void");
  3503.       }
  3504.  
  3505.       if (!hir_parameters.is_empty()) {
  3506.          YYLTYPE loc = this->get_location();
  3507.  
  3508.          _mesa_glsl_error(& loc, state, "main() must not take any parameters");
  3509.       }
  3510.    }
  3511.  
  3512.    /* Finish storing the information about this new function in its signature.
  3513.     */
  3514.    if (sig == NULL) {
  3515.       sig = new(ctx) ir_function_signature(return_type);
  3516.       f->add_signature(sig);
  3517.    }
  3518.  
  3519.    sig->replace_parameters(&hir_parameters);
  3520.    signature = sig;
  3521.  
  3522.    /* Function declarations (prototypes) do not have r-values.
  3523.     */
  3524.    return NULL;
  3525. }
  3526.  
  3527.  
  3528. ir_rvalue *
  3529. ast_function_definition::hir(exec_list *instructions,
  3530.                              struct _mesa_glsl_parse_state *state)
  3531. {
  3532.    prototype->is_definition = true;
  3533.    prototype->hir(instructions, state);
  3534.  
  3535.    ir_function_signature *signature = prototype->signature;
  3536.    if (signature == NULL)
  3537.       return NULL;
  3538.  
  3539.    assert(state->current_function == NULL);
  3540.    state->current_function = signature;
  3541.    state->found_return = false;
  3542.  
  3543.    /* Duplicate parameters declared in the prototype as concrete variables.
  3544.     * Add these to the symbol table.
  3545.     */
  3546.    state->symbols->push_scope();
  3547.    foreach_iter(exec_list_iterator, iter, signature->parameters) {
  3548.       ir_variable *const var = ((ir_instruction *) iter.get())->as_variable();
  3549.  
  3550.       assert(var != NULL);
  3551.  
  3552.       /* The only way a parameter would "exist" is if two parameters have
  3553.        * the same name.
  3554.        */
  3555.       if (state->symbols->name_declared_this_scope(var->name)) {
  3556.          YYLTYPE loc = this->get_location();
  3557.  
  3558.          _mesa_glsl_error(& loc, state, "parameter `%s' redeclared", var->name);
  3559.       } else {
  3560.          state->symbols->add_variable(var);
  3561.       }
  3562.    }
  3563.  
  3564.    /* Convert the body of the function to HIR. */
  3565.    this->body->hir(&signature->body, state);
  3566.    signature->is_defined = true;
  3567.  
  3568.    state->symbols->pop_scope();
  3569.  
  3570.    assert(state->current_function == signature);
  3571.    state->current_function = NULL;
  3572.  
  3573.    if (!signature->return_type->is_void() && !state->found_return) {
  3574.       YYLTYPE loc = this->get_location();
  3575.       _mesa_glsl_error(& loc, state, "function `%s' has non-void return type "
  3576.                        "%s, but no return statement",
  3577.                        signature->function_name(),
  3578.                        signature->return_type->name);
  3579.    }
  3580.  
  3581.    /* Function definitions do not have r-values.
  3582.     */
  3583.    return NULL;
  3584. }
  3585.  
  3586.  
  3587. ir_rvalue *
  3588. ast_jump_statement::hir(exec_list *instructions,
  3589.                         struct _mesa_glsl_parse_state *state)
  3590. {
  3591.    void *ctx = state;
  3592.  
  3593.    switch (mode) {
  3594.    case ast_return: {
  3595.       ir_return *inst;
  3596.       assert(state->current_function);
  3597.  
  3598.       if (opt_return_value) {
  3599.          ir_rvalue *ret = opt_return_value->hir(instructions, state);
  3600.  
  3601.          /* The value of the return type can be NULL if the shader says
  3602.           * 'return foo();' and foo() is a function that returns void.
  3603.           *
  3604.           * NOTE: The GLSL spec doesn't say that this is an error.  The type
  3605.           * of the return value is void.  If the return type of the function is
  3606.           * also void, then this should compile without error.  Seriously.
  3607.           */
  3608.          const glsl_type *const ret_type =
  3609.             (ret == NULL) ? glsl_type::void_type : ret->type;
  3610.  
  3611.          /* Implicit conversions are not allowed for return values prior to
  3612.           * ARB_shading_language_420pack.
  3613.           */
  3614.          if (state->current_function->return_type != ret_type) {
  3615.             YYLTYPE loc = this->get_location();
  3616.  
  3617.             if (state->ARB_shading_language_420pack_enable) {
  3618.                if (!apply_implicit_conversion(state->current_function->return_type,
  3619.                                               ret, state)) {
  3620.                   _mesa_glsl_error(& loc, state,
  3621.                                    "Could not implicitly convert return value "
  3622.                                    "to %s, in function `%s'",
  3623.                                    state->current_function->return_type->name,
  3624.                                    state->current_function->function_name());
  3625.                }
  3626.             } else {
  3627.                _mesa_glsl_error(& loc, state,
  3628.                                 "`return' with wrong type %s, in function `%s' "
  3629.                                 "returning %s",
  3630.                                 ret_type->name,
  3631.                                 state->current_function->function_name(),
  3632.                                 state->current_function->return_type->name);
  3633.             }
  3634.          } else if (state->current_function->return_type->base_type ==
  3635.                     GLSL_TYPE_VOID) {
  3636.             YYLTYPE loc = this->get_location();
  3637.  
  3638.             /* The ARB_shading_language_420pack, GLSL ES 3.0, and GLSL 4.20
  3639.              * specs add a clarification:
  3640.              *
  3641.              *    "A void function can only use return without a return argument, even if
  3642.              *     the return argument has void type. Return statements only accept values:
  3643.              *
  3644.              *         void func1() { }
  3645.              *         void func2() { return func1(); } // illegal return statement"
  3646.              */
  3647.             _mesa_glsl_error(& loc, state,
  3648.                              "void functions can only use `return' without a "
  3649.                              "return argument");
  3650.          }
  3651.  
  3652.          inst = new(ctx) ir_return(ret);
  3653.       } else {
  3654.          if (state->current_function->return_type->base_type !=
  3655.              GLSL_TYPE_VOID) {
  3656.             YYLTYPE loc = this->get_location();
  3657.  
  3658.             _mesa_glsl_error(& loc, state,
  3659.                              "`return' with no value, in function %s returning "
  3660.                              "non-void",
  3661.                              state->current_function->function_name());
  3662.          }
  3663.          inst = new(ctx) ir_return;
  3664.       }
  3665.  
  3666.       state->found_return = true;
  3667.       instructions->push_tail(inst);
  3668.       break;
  3669.    }
  3670.  
  3671.    case ast_discard:
  3672.       if (state->target != fragment_shader) {
  3673.          YYLTYPE loc = this->get_location();
  3674.  
  3675.          _mesa_glsl_error(& loc, state,
  3676.                           "`discard' may only appear in a fragment shader");
  3677.       }
  3678.       instructions->push_tail(new(ctx) ir_discard);
  3679.       break;
  3680.  
  3681.    case ast_break:
  3682.    case ast_continue:
  3683.       if (mode == ast_continue &&
  3684.           state->loop_nesting_ast == NULL) {
  3685.          YYLTYPE loc = this->get_location();
  3686.  
  3687.          _mesa_glsl_error(& loc, state,
  3688.                           "continue may only appear in a loop");
  3689.       } else if (mode == ast_break &&
  3690.                  state->loop_nesting_ast == NULL &&
  3691.                  state->switch_state.switch_nesting_ast == NULL) {
  3692.          YYLTYPE loc = this->get_location();
  3693.  
  3694.          _mesa_glsl_error(& loc, state,
  3695.                           "break may only appear in a loop or a switch");
  3696.       } else {
  3697.          /* For a loop, inline the for loop expression again,
  3698.           * since we don't know where near the end of
  3699.           * the loop body the normal copy of it
  3700.           * is going to be placed.
  3701.           */
  3702.          if (state->loop_nesting_ast != NULL &&
  3703.              mode == ast_continue &&
  3704.              state->loop_nesting_ast->rest_expression) {
  3705.             state->loop_nesting_ast->rest_expression->hir(instructions,
  3706.                                                           state);
  3707.          }
  3708.  
  3709.          if (state->switch_state.is_switch_innermost &&
  3710.              mode == ast_break) {
  3711.             /* Force break out of switch by setting is_break switch state.
  3712.              */
  3713.             ir_variable *const is_break_var = state->switch_state.is_break_var;
  3714.             ir_dereference_variable *const deref_is_break_var =
  3715.                new(ctx) ir_dereference_variable(is_break_var);
  3716.             ir_constant *const true_val = new(ctx) ir_constant(true);
  3717.             ir_assignment *const set_break_var =
  3718.                new(ctx) ir_assignment(deref_is_break_var, true_val);
  3719.            
  3720.             instructions->push_tail(set_break_var);
  3721.          }
  3722.          else {
  3723.             ir_loop_jump *const jump =
  3724.                new(ctx) ir_loop_jump((mode == ast_break)
  3725.                                      ? ir_loop_jump::jump_break
  3726.                                      : ir_loop_jump::jump_continue);
  3727.             instructions->push_tail(jump);
  3728.          }
  3729.       }
  3730.  
  3731.       break;
  3732.    }
  3733.  
  3734.    /* Jump instructions do not have r-values.
  3735.     */
  3736.    return NULL;
  3737. }
  3738.  
  3739.  
  3740. ir_rvalue *
  3741. ast_selection_statement::hir(exec_list *instructions,
  3742.                              struct _mesa_glsl_parse_state *state)
  3743. {
  3744.    void *ctx = state;
  3745.  
  3746.    ir_rvalue *const condition = this->condition->hir(instructions, state);
  3747.  
  3748.    /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec:
  3749.     *
  3750.     *    "Any expression whose type evaluates to a Boolean can be used as the
  3751.     *    conditional expression bool-expression. Vector types are not accepted
  3752.     *    as the expression to if."
  3753.     *
  3754.     * The checks are separated so that higher quality diagnostics can be
  3755.     * generated for cases where both rules are violated.
  3756.     */
  3757.    if (!condition->type->is_boolean() || !condition->type->is_scalar()) {
  3758.       YYLTYPE loc = this->condition->get_location();
  3759.  
  3760.       _mesa_glsl_error(& loc, state, "if-statement condition must be scalar "
  3761.                        "boolean");
  3762.    }
  3763.  
  3764.    ir_if *const stmt = new(ctx) ir_if(condition);
  3765.  
  3766.    if (then_statement != NULL) {
  3767.       state->symbols->push_scope();
  3768.       then_statement->hir(& stmt->then_instructions, state);
  3769.       state->symbols->pop_scope();
  3770.    }
  3771.  
  3772.    if (else_statement != NULL) {
  3773.       state->symbols->push_scope();
  3774.       else_statement->hir(& stmt->else_instructions, state);
  3775.       state->symbols->pop_scope();
  3776.    }
  3777.  
  3778.    instructions->push_tail(stmt);
  3779.  
  3780.    /* if-statements do not have r-values.
  3781.     */
  3782.    return NULL;
  3783. }
  3784.  
  3785.  
  3786. ir_rvalue *
  3787. ast_switch_statement::hir(exec_list *instructions,
  3788.                           struct _mesa_glsl_parse_state *state)
  3789. {
  3790.    void *ctx = state;
  3791.  
  3792.    ir_rvalue *const test_expression =
  3793.       this->test_expression->hir(instructions, state);
  3794.  
  3795.    /* From page 66 (page 55 of the PDF) of the GLSL 1.50 spec:
  3796.     *
  3797.     *    "The type of init-expression in a switch statement must be a
  3798.     *     scalar integer."
  3799.     */
  3800.    if (!test_expression->type->is_scalar() ||
  3801.        !test_expression->type->is_integer()) {
  3802.       YYLTYPE loc = this->test_expression->get_location();
  3803.  
  3804.       _mesa_glsl_error(& loc,
  3805.                        state,
  3806.                        "switch-statement expression must be scalar "
  3807.                        "integer");
  3808.    }
  3809.  
  3810.    /* Track the switch-statement nesting in a stack-like manner.
  3811.     */
  3812.    struct glsl_switch_state saved = state->switch_state;
  3813.  
  3814.    state->switch_state.is_switch_innermost = true;
  3815.    state->switch_state.switch_nesting_ast = this;
  3816.    state->switch_state.labels_ht = hash_table_ctor(0, hash_table_pointer_hash,
  3817.                                                    hash_table_pointer_compare);
  3818.    state->switch_state.previous_default = NULL;
  3819.  
  3820.    /* Initalize is_fallthru state to false.
  3821.     */
  3822.    ir_rvalue *const is_fallthru_val = new (ctx) ir_constant(false);
  3823.    state->switch_state.is_fallthru_var =
  3824.       new(ctx) ir_variable(glsl_type::bool_type,
  3825.                            "switch_is_fallthru_tmp",
  3826.                            ir_var_temporary);
  3827.    instructions->push_tail(state->switch_state.is_fallthru_var);
  3828.  
  3829.    ir_dereference_variable *deref_is_fallthru_var =
  3830.       new(ctx) ir_dereference_variable(state->switch_state.is_fallthru_var);
  3831.    instructions->push_tail(new(ctx) ir_assignment(deref_is_fallthru_var,
  3832.                                                   is_fallthru_val));
  3833.  
  3834.    /* Initalize is_break state to false.
  3835.     */
  3836.    ir_rvalue *const is_break_val = new (ctx) ir_constant(false);
  3837.    state->switch_state.is_break_var = new(ctx) ir_variable(glsl_type::bool_type,
  3838.                                                            "switch_is_break_tmp",
  3839.                                                            ir_var_temporary);
  3840.    instructions->push_tail(state->switch_state.is_break_var);
  3841.  
  3842.    ir_dereference_variable *deref_is_break_var =
  3843.       new(ctx) ir_dereference_variable(state->switch_state.is_break_var);
  3844.    instructions->push_tail(new(ctx) ir_assignment(deref_is_break_var,
  3845.                                                   is_break_val));
  3846.  
  3847.    /* Cache test expression.
  3848.     */
  3849.    test_to_hir(instructions, state);
  3850.  
  3851.    /* Emit code for body of switch stmt.
  3852.     */
  3853.    body->hir(instructions, state);
  3854.  
  3855.    hash_table_dtor(state->switch_state.labels_ht);
  3856.  
  3857.    state->switch_state = saved;
  3858.  
  3859.    /* Switch statements do not have r-values. */
  3860.    return NULL;
  3861. }
  3862.  
  3863.  
  3864. void
  3865. ast_switch_statement::test_to_hir(exec_list *instructions,
  3866.                                   struct _mesa_glsl_parse_state *state)
  3867. {
  3868.    void *ctx = state;
  3869.  
  3870.    /* Cache value of test expression. */
  3871.    ir_rvalue *const test_val =
  3872.       test_expression->hir(instructions,
  3873.                            state);
  3874.  
  3875.    state->switch_state.test_var = new(ctx) ir_variable(test_val->type,
  3876.                                                        "switch_test_tmp",
  3877.                                                        ir_var_temporary);
  3878.    ir_dereference_variable *deref_test_var =
  3879.       new(ctx) ir_dereference_variable(state->switch_state.test_var);
  3880.  
  3881.    instructions->push_tail(state->switch_state.test_var);
  3882.    instructions->push_tail(new(ctx) ir_assignment(deref_test_var, test_val));
  3883. }
  3884.  
  3885.  
  3886. ir_rvalue *
  3887. ast_switch_body::hir(exec_list *instructions,
  3888.                      struct _mesa_glsl_parse_state *state)
  3889. {
  3890.    if (stmts != NULL)
  3891.       stmts->hir(instructions, state);
  3892.  
  3893.    /* Switch bodies do not have r-values. */
  3894.    return NULL;
  3895. }
  3896.  
  3897. ir_rvalue *
  3898. ast_case_statement_list::hir(exec_list *instructions,
  3899.                              struct _mesa_glsl_parse_state *state)
  3900. {
  3901.    foreach_list_typed (ast_case_statement, case_stmt, link, & this->cases)
  3902.       case_stmt->hir(instructions, state);
  3903.  
  3904.    /* Case statements do not have r-values. */
  3905.    return NULL;
  3906. }
  3907.  
  3908. ir_rvalue *
  3909. ast_case_statement::hir(exec_list *instructions,
  3910.                         struct _mesa_glsl_parse_state *state)
  3911. {
  3912.    labels->hir(instructions, state);
  3913.  
  3914.    /* Conditionally set fallthru state based on break state. */
  3915.    ir_constant *const false_val = new(state) ir_constant(false);
  3916.    ir_dereference_variable *const deref_is_fallthru_var =
  3917.       new(state) ir_dereference_variable(state->switch_state.is_fallthru_var);
  3918.    ir_dereference_variable *const deref_is_break_var =
  3919.       new(state) ir_dereference_variable(state->switch_state.is_break_var);
  3920.    ir_assignment *const reset_fallthru_on_break =
  3921.       new(state) ir_assignment(deref_is_fallthru_var,
  3922.                                false_val,
  3923.                                deref_is_break_var);
  3924.    instructions->push_tail(reset_fallthru_on_break);
  3925.  
  3926.    /* Guard case statements depending on fallthru state. */
  3927.    ir_dereference_variable *const deref_fallthru_guard =
  3928.       new(state) ir_dereference_variable(state->switch_state.is_fallthru_var);
  3929.    ir_if *const test_fallthru = new(state) ir_if(deref_fallthru_guard);
  3930.  
  3931.    foreach_list_typed (ast_node, stmt, link, & this->stmts)
  3932.       stmt->hir(& test_fallthru->then_instructions, state);
  3933.  
  3934.    instructions->push_tail(test_fallthru);
  3935.  
  3936.    /* Case statements do not have r-values. */
  3937.    return NULL;
  3938. }
  3939.  
  3940.  
  3941. ir_rvalue *
  3942. ast_case_label_list::hir(exec_list *instructions,
  3943.                          struct _mesa_glsl_parse_state *state)
  3944. {
  3945.    foreach_list_typed (ast_case_label, label, link, & this->labels)
  3946.       label->hir(instructions, state);
  3947.  
  3948.    /* Case labels do not have r-values. */
  3949.    return NULL;
  3950. }
  3951.  
  3952. ir_rvalue *
  3953. ast_case_label::hir(exec_list *instructions,
  3954.                     struct _mesa_glsl_parse_state *state)
  3955. {
  3956.    void *ctx = state;
  3957.  
  3958.    ir_dereference_variable *deref_fallthru_var =
  3959.       new(ctx) ir_dereference_variable(state->switch_state.is_fallthru_var);
  3960.  
  3961.    ir_rvalue *const true_val = new(ctx) ir_constant(true);
  3962.  
  3963.    /* If not default case, ... */
  3964.    if (this->test_value != NULL) {
  3965.       /* Conditionally set fallthru state based on
  3966.        * comparison of cached test expression value to case label.
  3967.        */
  3968.       ir_rvalue *const label_rval = this->test_value->hir(instructions, state);
  3969.       ir_constant *label_const = label_rval->constant_expression_value();
  3970.  
  3971.       if (!label_const) {
  3972.          YYLTYPE loc = this->test_value->get_location();
  3973.  
  3974.          _mesa_glsl_error(& loc, state,
  3975.                           "switch statement case label must be a "
  3976.                           "constant expression");
  3977.  
  3978.          /* Stuff a dummy value in to allow processing to continue. */
  3979.          label_const = new(ctx) ir_constant(0);
  3980.       } else {
  3981.          ast_expression *previous_label = (ast_expression *)
  3982.             hash_table_find(state->switch_state.labels_ht,
  3983.                             (void *)(uintptr_t)label_const->value.u[0]);
  3984.  
  3985.          if (previous_label) {
  3986.             YYLTYPE loc = this->test_value->get_location();
  3987.             _mesa_glsl_error(& loc, state,
  3988.                              "duplicate case value");
  3989.  
  3990.             loc = previous_label->get_location();
  3991.             _mesa_glsl_error(& loc, state,
  3992.                              "this is the previous case label");
  3993.          } else {
  3994.             hash_table_insert(state->switch_state.labels_ht,
  3995.                               this->test_value,
  3996.                               (void *)(uintptr_t)label_const->value.u[0]);
  3997.          }
  3998.       }
  3999.  
  4000.       ir_dereference_variable *deref_test_var =
  4001.          new(ctx) ir_dereference_variable(state->switch_state.test_var);
  4002.  
  4003.       ir_rvalue *const test_cond = new(ctx) ir_expression(ir_binop_all_equal,
  4004.                                                           label_const,
  4005.                                                           deref_test_var);
  4006.  
  4007.       ir_assignment *set_fallthru_on_test =
  4008.          new(ctx) ir_assignment(deref_fallthru_var,
  4009.                                 true_val,
  4010.                                 test_cond);
  4011.  
  4012.       instructions->push_tail(set_fallthru_on_test);
  4013.    } else { /* default case */
  4014.       if (state->switch_state.previous_default) {
  4015.          YYLTYPE loc = this->get_location();
  4016.          _mesa_glsl_error(& loc, state,
  4017.                           "multiple default labels in one switch");
  4018.  
  4019.          loc = state->switch_state.previous_default->get_location();
  4020.          _mesa_glsl_error(& loc, state,
  4021.                           "this is the first default label");
  4022.       }
  4023.       state->switch_state.previous_default = this;
  4024.  
  4025.       /* Set falltrhu state. */
  4026.       ir_assignment *set_fallthru =
  4027.          new(ctx) ir_assignment(deref_fallthru_var, true_val);
  4028.  
  4029.       instructions->push_tail(set_fallthru);
  4030.    }
  4031.  
  4032.    /* Case statements do not have r-values. */
  4033.    return NULL;
  4034. }
  4035.  
  4036. void
  4037. ast_iteration_statement::condition_to_hir(ir_loop *stmt,
  4038.                                           struct _mesa_glsl_parse_state *state)
  4039. {
  4040.    void *ctx = state;
  4041.  
  4042.    if (condition != NULL) {
  4043.       ir_rvalue *const cond =
  4044.          condition->hir(& stmt->body_instructions, state);
  4045.  
  4046.       if ((cond == NULL)
  4047.           || !cond->type->is_boolean() || !cond->type->is_scalar()) {
  4048.          YYLTYPE loc = condition->get_location();
  4049.  
  4050.          _mesa_glsl_error(& loc, state,
  4051.                           "loop condition must be scalar boolean");
  4052.       } else {
  4053.          /* As the first code in the loop body, generate a block that looks
  4054.           * like 'if (!condition) break;' as the loop termination condition.
  4055.           */
  4056.          ir_rvalue *const not_cond =
  4057.             new(ctx) ir_expression(ir_unop_logic_not, cond);
  4058.  
  4059.          ir_if *const if_stmt = new(ctx) ir_if(not_cond);
  4060.  
  4061.          ir_jump *const break_stmt =
  4062.             new(ctx) ir_loop_jump(ir_loop_jump::jump_break);
  4063.  
  4064.          if_stmt->then_instructions.push_tail(break_stmt);
  4065.          stmt->body_instructions.push_tail(if_stmt);
  4066.       }
  4067.    }
  4068. }
  4069.  
  4070.  
  4071. ir_rvalue *
  4072. ast_iteration_statement::hir(exec_list *instructions,
  4073.                              struct _mesa_glsl_parse_state *state)
  4074. {
  4075.    void *ctx = state;
  4076.  
  4077.    /* For-loops and while-loops start a new scope, but do-while loops do not.
  4078.     */
  4079.    if (mode != ast_do_while)
  4080.       state->symbols->push_scope();
  4081.  
  4082.    if (init_statement != NULL)
  4083.       init_statement->hir(instructions, state);
  4084.  
  4085.    ir_loop *const stmt = new(ctx) ir_loop();
  4086.    instructions->push_tail(stmt);
  4087.  
  4088.    /* Track the current loop nesting. */
  4089.    ast_iteration_statement *nesting_ast = state->loop_nesting_ast;
  4090.  
  4091.    state->loop_nesting_ast = this;
  4092.  
  4093.    /* Likewise, indicate that following code is closest to a loop,
  4094.     * NOT closest to a switch.
  4095.     */
  4096.    bool saved_is_switch_innermost = state->switch_state.is_switch_innermost;
  4097.    state->switch_state.is_switch_innermost = false;
  4098.  
  4099.    if (mode != ast_do_while)
  4100.       condition_to_hir(stmt, state);
  4101.  
  4102.    if (body != NULL)
  4103.       body->hir(& stmt->body_instructions, state);
  4104.  
  4105.    if (rest_expression != NULL)
  4106.       rest_expression->hir(& stmt->body_instructions, state);
  4107.  
  4108.    if (mode == ast_do_while)
  4109.       condition_to_hir(stmt, state);
  4110.  
  4111.    if (mode != ast_do_while)
  4112.       state->symbols->pop_scope();
  4113.  
  4114.    /* Restore previous nesting before returning. */
  4115.    state->loop_nesting_ast = nesting_ast;
  4116.    state->switch_state.is_switch_innermost = saved_is_switch_innermost;
  4117.  
  4118.    /* Loops do not have r-values.
  4119.     */
  4120.    return NULL;
  4121. }
  4122.  
  4123.  
  4124. /**
  4125.  * Determine if the given type is valid for establishing a default precision
  4126.  * qualifier.
  4127.  *
  4128.  * From GLSL ES 3.00 section 4.5.4 ("Default Precision Qualifiers"):
  4129.  *
  4130.  *     "The precision statement
  4131.  *
  4132.  *         precision precision-qualifier type;
  4133.  *
  4134.  *     can be used to establish a default precision qualifier. The type field
  4135.  *     can be either int or float or any of the sampler types, and the
  4136.  *     precision-qualifier can be lowp, mediump, or highp."
  4137.  *
  4138.  * GLSL ES 1.00 has similar language.  GLSL 1.30 doesn't allow precision
  4139.  * qualifiers on sampler types, but this seems like an oversight (since the
  4140.  * intention of including these in GLSL 1.30 is to allow compatibility with ES
  4141.  * shaders).  So we allow int, float, and all sampler types regardless of GLSL
  4142.  * version.
  4143.  */
  4144. static bool
  4145. is_valid_default_precision_type(const struct glsl_type *const type)
  4146. {
  4147.    if (type == NULL)
  4148.       return false;
  4149.  
  4150.    switch (type->base_type) {
  4151.    case GLSL_TYPE_INT:
  4152.    case GLSL_TYPE_FLOAT:
  4153.       /* "int" and "float" are valid, but vectors and matrices are not. */
  4154.       return type->vector_elements == 1 && type->matrix_columns == 1;
  4155.    case GLSL_TYPE_SAMPLER:
  4156.       return true;
  4157.    default:
  4158.       return false;
  4159.    }
  4160. }
  4161.  
  4162.  
  4163. ir_rvalue *
  4164. ast_type_specifier::hir(exec_list *instructions,
  4165.                           struct _mesa_glsl_parse_state *state)
  4166. {
  4167.    if (this->default_precision == ast_precision_none && this->structure == NULL)
  4168.       return NULL;
  4169.  
  4170.    YYLTYPE loc = this->get_location();
  4171.  
  4172.    /* If this is a precision statement, check that the type to which it is
  4173.     * applied is either float or int.
  4174.     *
  4175.     * From section 4.5.3 of the GLSL 1.30 spec:
  4176.     *    "The precision statement
  4177.     *       precision precision-qualifier type;
  4178.     *    can be used to establish a default precision qualifier. The type
  4179.     *    field can be either int or float [...].  Any other types or
  4180.     *    qualifiers will result in an error.
  4181.     */
  4182.    if (this->default_precision != ast_precision_none) {
  4183.       if (!state->check_precision_qualifiers_allowed(&loc))
  4184.          return NULL;
  4185.  
  4186.       if (this->structure != NULL) {
  4187.          _mesa_glsl_error(&loc, state,
  4188.                           "precision qualifiers do not apply to structures");
  4189.          return NULL;
  4190.       }
  4191.  
  4192.       if (this->is_array) {
  4193.          _mesa_glsl_error(&loc, state,
  4194.                           "default precision statements do not apply to "
  4195.                           "arrays");
  4196.          return NULL;
  4197.       }
  4198.  
  4199.       const struct glsl_type *const type =
  4200.          state->symbols->get_type(this->type_name);
  4201.       if (!is_valid_default_precision_type(type)) {
  4202.          _mesa_glsl_error(&loc, state,
  4203.                           "default precision statements apply only to types "
  4204.                           "float, int, and sampler types");
  4205.          return NULL;
  4206.       }
  4207.  
  4208.       if (type->base_type == GLSL_TYPE_FLOAT
  4209.           && state->es_shader
  4210.           && state->target == fragment_shader) {
  4211.          /* Section 4.5.3 (Default Precision Qualifiers) of the GLSL ES 1.00
  4212.           * spec says:
  4213.           *
  4214.           *     "The fragment language has no default precision qualifier for
  4215.           *     floating point types."
  4216.           *
  4217.           * As a result, we have to track whether or not default precision has
  4218.           * been specified for float in GLSL ES fragment shaders.
  4219.           *
  4220.           * Earlier in that same section, the spec says:
  4221.           *
  4222.           *     "Non-precision qualified declarations will use the precision
  4223.           *     qualifier specified in the most recent precision statement
  4224.           *     that is still in scope. The precision statement has the same
  4225.           *     scoping rules as variable declarations. If it is declared
  4226.           *     inside a compound statement, its effect stops at the end of
  4227.           *     the innermost statement it was declared in. Precision
  4228.           *     statements in nested scopes override precision statements in
  4229.           *     outer scopes. Multiple precision statements for the same basic
  4230.           *     type can appear inside the same scope, with later statements
  4231.           *     overriding earlier statements within that scope."
  4232.           *
  4233.           * Default precision specifications follow the same scope rules as
  4234.           * variables.  So, we can track the state of the default float
  4235.           * precision in the symbol table, and the rules will just work.  This
  4236.           * is a slight abuse of the symbol table, but it has the semantics
  4237.           * that we want.
  4238.           */
  4239.          ir_variable *const junk =
  4240.             new(state) ir_variable(type, "#default precision",
  4241.                                    ir_var_temporary);
  4242.  
  4243.          state->symbols->add_variable(junk);
  4244.       }
  4245.  
  4246.       /* FINISHME: Translate precision statements into IR. */
  4247.       return NULL;
  4248.    }
  4249.  
  4250.    /* _mesa_ast_set_aggregate_type() sets the <structure> field so that
  4251.     * process_record_constructor() can do type-checking on C-style initializer
  4252.     * expressions of structs, but ast_struct_specifier should only be translated
  4253.     * to HIR if it is declaring the type of a structure.
  4254.     *
  4255.     * The ->is_declaration field is false for initializers of variables
  4256.     * declared separately from the struct's type definition.
  4257.     *
  4258.     *    struct S { ... };              (is_declaration = true)
  4259.     *    struct T { ... } t = { ... };  (is_declaration = true)
  4260.     *    S s = { ... };                 (is_declaration = false)
  4261.     */
  4262.    if (this->structure != NULL && this->structure->is_declaration)
  4263.       return this->structure->hir(instructions, state);
  4264.  
  4265.    return NULL;
  4266. }
  4267.  
  4268.  
  4269. /**
  4270.  * Process a structure or interface block tree into an array of structure fields
  4271.  *
  4272.  * After parsing, where there are some syntax differnces, structures and
  4273.  * interface blocks are almost identical.  They are similar enough that the
  4274.  * AST for each can be processed the same way into a set of
  4275.  * \c glsl_struct_field to describe the members.
  4276.  *
  4277.  * \return
  4278.  * The number of fields processed.  A pointer to the array structure fields is
  4279.  * stored in \c *fields_ret.
  4280.  */
  4281. unsigned
  4282. ast_process_structure_or_interface_block(exec_list *instructions,
  4283.                                          struct _mesa_glsl_parse_state *state,
  4284.                                          exec_list *declarations,
  4285.                                          YYLTYPE &loc,
  4286.                                          glsl_struct_field **fields_ret,
  4287.                                          bool is_interface,
  4288.                                          bool block_row_major)
  4289. {
  4290.    unsigned decl_count = 0;
  4291.  
  4292.    /* Make an initial pass over the list of fields to determine how
  4293.     * many there are.  Each element in this list is an ast_declarator_list.
  4294.     * This means that we actually need to count the number of elements in the
  4295.     * 'declarations' list in each of the elements.
  4296.     */
  4297.    foreach_list_typed (ast_declarator_list, decl_list, link, declarations) {
  4298.       foreach_list_const (decl_ptr, & decl_list->declarations) {
  4299.          decl_count++;
  4300.       }
  4301.    }
  4302.  
  4303.    /* Allocate storage for the fields and process the field
  4304.     * declarations.  As the declarations are processed, try to also convert
  4305.     * the types to HIR.  This ensures that structure definitions embedded in
  4306.     * other structure definitions or in interface blocks are processed.
  4307.     */
  4308.    glsl_struct_field *const fields = ralloc_array(state, glsl_struct_field,
  4309.                                                   decl_count);
  4310.  
  4311.    unsigned i = 0;
  4312.    foreach_list_typed (ast_declarator_list, decl_list, link, declarations) {
  4313.       const char *type_name;
  4314.  
  4315.       decl_list->type->specifier->hir(instructions, state);
  4316.  
  4317.       /* Section 10.9 of the GLSL ES 1.00 specification states that
  4318.        * embedded structure definitions have been removed from the language.
  4319.        */
  4320.       if (state->es_shader && decl_list->type->specifier->structure != NULL) {
  4321.          _mesa_glsl_error(&loc, state, "Embedded structure definitions are "
  4322.                           "not allowed in GLSL ES 1.00.");
  4323.       }
  4324.  
  4325.       const glsl_type *decl_type =
  4326.          decl_list->type->glsl_type(& type_name, state);
  4327.  
  4328.       foreach_list_typed (ast_declaration, decl, link,
  4329.                           &decl_list->declarations) {
  4330.          /* From the GL_ARB_uniform_buffer_object spec:
  4331.           *
  4332.           *     "Sampler types are not allowed inside of uniform
  4333.           *      blocks. All other types, arrays, and structures
  4334.           *      allowed for uniforms are allowed within a uniform
  4335.           *      block."
  4336.           *
  4337.           * It should be impossible for decl_type to be NULL here.  Cases that
  4338.           * might naturally lead to decl_type being NULL, especially for the
  4339.           * is_interface case, will have resulted in compilation having
  4340.           * already halted due to a syntax error.
  4341.           */
  4342.          const struct glsl_type *field_type =
  4343.             decl_type != NULL ? decl_type : glsl_type::error_type;
  4344.  
  4345.          if (is_interface && field_type->contains_sampler()) {
  4346.             YYLTYPE loc = decl_list->get_location();
  4347.             _mesa_glsl_error(&loc, state,
  4348.                              "Uniform in non-default uniform block contains sampler\n");
  4349.          }
  4350.  
  4351.          const struct ast_type_qualifier *const qual =
  4352.             & decl_list->type->qualifier;
  4353.          if (qual->flags.q.std140 ||
  4354.              qual->flags.q.packed ||
  4355.              qual->flags.q.shared) {
  4356.             _mesa_glsl_error(&loc, state,
  4357.                              "uniform block layout qualifiers std140, packed, and "
  4358.                              "shared can only be applied to uniform blocks, not "
  4359.                              "members");
  4360.          }
  4361.  
  4362.          if (decl->is_array) {
  4363.             field_type = process_array_type(&loc, decl_type, decl->array_size,
  4364.                                             state);
  4365.          }
  4366.          fields[i].type = field_type;
  4367.          fields[i].name = decl->identifier;
  4368.  
  4369.          if (qual->flags.q.row_major || qual->flags.q.column_major) {
  4370.             if (!qual->flags.q.uniform) {
  4371.                _mesa_glsl_error(&loc, state,
  4372.                                 "row_major and column_major can only be "
  4373.                                 "applied to uniform interface blocks");
  4374.             } else
  4375.                validate_matrix_layout_for_type(state, &loc, field_type, NULL);
  4376.          }
  4377.  
  4378.          if (qual->flags.q.uniform && qual->has_interpolation()) {
  4379.             _mesa_glsl_error(&loc, state,
  4380.                              "interpolation qualifiers cannot be used "
  4381.                              "with uniform interface blocks");
  4382.          }
  4383.  
  4384.          if (field_type->is_matrix() ||
  4385.              (field_type->is_array() && field_type->fields.array->is_matrix())) {
  4386.             fields[i].row_major = block_row_major;
  4387.             if (qual->flags.q.row_major)
  4388.                fields[i].row_major = true;
  4389.             else if (qual->flags.q.column_major)
  4390.                fields[i].row_major = false;
  4391.          }
  4392.  
  4393.          i++;
  4394.       }
  4395.    }
  4396.  
  4397.    assert(i == decl_count);
  4398.  
  4399.    *fields_ret = fields;
  4400.    return decl_count;
  4401. }
  4402.  
  4403.  
  4404. ir_rvalue *
  4405. ast_struct_specifier::hir(exec_list *instructions,
  4406.                           struct _mesa_glsl_parse_state *state)
  4407. {
  4408.    YYLTYPE loc = this->get_location();
  4409.  
  4410.    /* Section 4.1.8 (Structures) of the GLSL 1.10 spec says:
  4411.     *
  4412.     *     "Anonymous structures are not supported; so embedded structures must
  4413.     *     have a declarator. A name given to an embedded struct is scoped at
  4414.     *     the same level as the struct it is embedded in."
  4415.     *
  4416.     * The same section of the  GLSL 1.20 spec says:
  4417.     *
  4418.     *     "Anonymous structures are not supported. Embedded structures are not
  4419.     *     supported.
  4420.     *
  4421.     *         struct S { float f; };
  4422.     *         struct T {
  4423.     *             S;              // Error: anonymous structures disallowed
  4424.     *             struct { ... }; // Error: embedded structures disallowed
  4425.     *             S s;            // Okay: nested structures with name are allowed
  4426.     *         };"
  4427.     *
  4428.     * The GLSL ES 1.00 and 3.00 specs have similar langauge and examples.  So,
  4429.     * we allow embedded structures in 1.10 only.
  4430.     */
  4431.    if (state->language_version != 110 && state->struct_specifier_depth != 0)
  4432.       _mesa_glsl_error(&loc, state,
  4433.                        "embedded structure declartions are not allowed");
  4434.  
  4435.    state->struct_specifier_depth++;
  4436.  
  4437.    glsl_struct_field *fields;
  4438.    unsigned decl_count =
  4439.       ast_process_structure_or_interface_block(instructions,
  4440.                                                state,
  4441.                                                &this->declarations,
  4442.                                                loc,
  4443.                                                &fields,
  4444.                                                false,
  4445.                                                false);
  4446.  
  4447.    const glsl_type *t =
  4448.       glsl_type::get_record_instance(fields, decl_count, this->name);
  4449.  
  4450.    if (!state->symbols->add_type(name, t)) {
  4451.       _mesa_glsl_error(& loc, state, "struct `%s' previously defined", name);
  4452.    } else {
  4453.       const glsl_type **s = reralloc(state, state->user_structures,
  4454.                                      const glsl_type *,
  4455.                                      state->num_user_structures + 1);
  4456.       if (s != NULL) {
  4457.          s[state->num_user_structures] = t;
  4458.          state->user_structures = s;
  4459.          state->num_user_structures++;
  4460.       }
  4461.    }
  4462.  
  4463.    state->struct_specifier_depth--;
  4464.  
  4465.    /* Structure type definitions do not have r-values.
  4466.     */
  4467.    return NULL;
  4468. }
  4469.  
  4470. ir_rvalue *
  4471. ast_interface_block::hir(exec_list *instructions,
  4472.                           struct _mesa_glsl_parse_state *state)
  4473. {
  4474.    YYLTYPE loc = this->get_location();
  4475.  
  4476.    /* The ast_interface_block has a list of ast_declarator_lists.  We
  4477.     * need to turn those into ir_variables with an association
  4478.     * with this uniform block.
  4479.     */
  4480.    enum glsl_interface_packing packing;
  4481.    if (this->layout.flags.q.shared) {
  4482.       packing = GLSL_INTERFACE_PACKING_SHARED;
  4483.    } else if (this->layout.flags.q.packed) {
  4484.       packing = GLSL_INTERFACE_PACKING_PACKED;
  4485.    } else {
  4486.       /* The default layout is std140.
  4487.        */
  4488.       packing = GLSL_INTERFACE_PACKING_STD140;
  4489.    }
  4490.  
  4491.    bool block_row_major = this->layout.flags.q.row_major;
  4492.    exec_list declared_variables;
  4493.    glsl_struct_field *fields;
  4494.    unsigned int num_variables =
  4495.       ast_process_structure_or_interface_block(&declared_variables,
  4496.                                                state,
  4497.                                                &this->declarations,
  4498.                                                loc,
  4499.                                                &fields,
  4500.                                                true,
  4501.                                                block_row_major);
  4502.  
  4503.    ir_variable_mode var_mode;
  4504.    const char *iface_type_name;
  4505.    if (this->layout.flags.q.in) {
  4506.       var_mode = ir_var_shader_in;
  4507.       iface_type_name = "in";
  4508.    } else if (this->layout.flags.q.out) {
  4509.       var_mode = ir_var_shader_out;
  4510.       iface_type_name = "out";
  4511.    } else if (this->layout.flags.q.uniform) {
  4512.       var_mode = ir_var_uniform;
  4513.       iface_type_name = "uniform";
  4514.    } else {
  4515.       var_mode = ir_var_auto;
  4516.       iface_type_name = "UNKNOWN";
  4517.       assert(!"interface block layout qualifier not found!");
  4518.    }
  4519.  
  4520.    const glsl_type *block_type =
  4521.       glsl_type::get_interface_instance(fields,
  4522.                                         num_variables,
  4523.                                         packing,
  4524.                                         this->block_name);
  4525.  
  4526.    if (!state->symbols->add_interface(block_type->name, block_type, var_mode)) {
  4527.       YYLTYPE loc = this->get_location();
  4528.       _mesa_glsl_error(&loc, state, "Interface block `%s' with type `%s' "
  4529.                        "already taken in the current scope.\n",
  4530.                        this->block_name, iface_type_name);
  4531.    }
  4532.  
  4533.    /* Since interface blocks cannot contain statements, it should be
  4534.     * impossible for the block to generate any instructions.
  4535.     */
  4536.    assert(declared_variables.is_empty());
  4537.  
  4538.    /* Page 39 (page 45 of the PDF) of section 4.3.7 in the GLSL ES 3.00 spec
  4539.     * says:
  4540.     *
  4541.     *     "If an instance name (instance-name) is used, then it puts all the
  4542.     *     members inside a scope within its own name space, accessed with the
  4543.     *     field selector ( . ) operator (analogously to structures)."
  4544.     */
  4545.    if (this->instance_name) {
  4546.       ir_variable *var;
  4547.  
  4548.       if (this->array_size != NULL) {
  4549.          const glsl_type *block_array_type =
  4550.             process_array_type(&loc, block_type, this->array_size, state);
  4551.  
  4552.          var = new(state) ir_variable(block_array_type,
  4553.                                       this->instance_name,
  4554.                                       var_mode);
  4555.       } else {
  4556.          var = new(state) ir_variable(block_type,
  4557.                                       this->instance_name,
  4558.                                       var_mode);
  4559.       }
  4560.  
  4561.       var->interface_type = block_type;
  4562.       state->symbols->add_variable(var);
  4563.       instructions->push_tail(var);
  4564.    } else {
  4565.       /* In order to have an array size, the block must also be declared with
  4566.        * an instane name.
  4567.        */
  4568.       assert(this->array_size == NULL);
  4569.  
  4570.       for (unsigned i = 0; i < num_variables; i++) {
  4571.          ir_variable *var =
  4572.             new(state) ir_variable(fields[i].type,
  4573.                                    ralloc_strdup(state, fields[i].name),
  4574.                                    var_mode);
  4575.          var->interface_type = block_type;
  4576.  
  4577.          /* Propagate the "binding" keyword into this UBO's fields;
  4578.           * the UBO declaration itself doesn't get an ir_variable unless it
  4579.           * has an instance name.  This is ugly.
  4580.           */
  4581.          var->explicit_binding = this->layout.flags.q.explicit_binding;
  4582.          var->binding = this->layout.binding;
  4583.  
  4584.          state->symbols->add_variable(var);
  4585.          instructions->push_tail(var);
  4586.       }
  4587.    }
  4588.  
  4589.    return NULL;
  4590. }
  4591.  
  4592. static void
  4593. detect_conflicting_assignments(struct _mesa_glsl_parse_state *state,
  4594.                                exec_list *instructions)
  4595. {
  4596.    bool gl_FragColor_assigned = false;
  4597.    bool gl_FragData_assigned = false;
  4598.    bool user_defined_fs_output_assigned = false;
  4599.    ir_variable *user_defined_fs_output = NULL;
  4600.  
  4601.    /* It would be nice to have proper location information. */
  4602.    YYLTYPE loc;
  4603.    memset(&loc, 0, sizeof(loc));
  4604.  
  4605.    foreach_list(node, instructions) {
  4606.       ir_variable *var = ((ir_instruction *)node)->as_variable();
  4607.  
  4608.       if (!var || !var->assigned)
  4609.          continue;
  4610.  
  4611.       if (strcmp(var->name, "gl_FragColor") == 0)
  4612.          gl_FragColor_assigned = true;
  4613.       else if (strcmp(var->name, "gl_FragData") == 0)
  4614.          gl_FragData_assigned = true;
  4615.       else if (strncmp(var->name, "gl_", 3) != 0) {
  4616.          if (state->target == fragment_shader &&
  4617.              var->mode == ir_var_shader_out) {
  4618.             user_defined_fs_output_assigned = true;
  4619.             user_defined_fs_output = var;
  4620.          }
  4621.       }
  4622.    }
  4623.  
  4624.    /* From the GLSL 1.30 spec:
  4625.     *
  4626.     *     "If a shader statically assigns a value to gl_FragColor, it
  4627.     *      may not assign a value to any element of gl_FragData. If a
  4628.     *      shader statically writes a value to any element of
  4629.     *      gl_FragData, it may not assign a value to
  4630.     *      gl_FragColor. That is, a shader may assign values to either
  4631.     *      gl_FragColor or gl_FragData, but not both. Multiple shaders
  4632.     *      linked together must also consistently write just one of
  4633.     *      these variables.  Similarly, if user declared output
  4634.     *      variables are in use (statically assigned to), then the
  4635.     *      built-in variables gl_FragColor and gl_FragData may not be
  4636.     *      assigned to. These incorrect usages all generate compile
  4637.     *      time errors."
  4638.     */
  4639.    if (gl_FragColor_assigned && gl_FragData_assigned) {
  4640.       _mesa_glsl_error(&loc, state, "fragment shader writes to both "
  4641.                        "`gl_FragColor' and `gl_FragData'\n");
  4642.    } else if (gl_FragColor_assigned && user_defined_fs_output_assigned) {
  4643.       _mesa_glsl_error(&loc, state, "fragment shader writes to both "
  4644.                        "`gl_FragColor' and `%s'\n",
  4645.                        user_defined_fs_output->name);
  4646.    } else if (gl_FragData_assigned && user_defined_fs_output_assigned) {
  4647.       _mesa_glsl_error(&loc, state, "fragment shader writes to both "
  4648.                        "`gl_FragData' and `%s'\n",
  4649.                        user_defined_fs_output->name);
  4650.    }
  4651. }
  4652.