1a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick/*
2a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * Copyright © 2010 Intel Corporation
3a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick *
4a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * Permission is hereby granted, free of charge, to any person obtaining a
5a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * copy of this software and associated documentation files (the "Software"),
6a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * to deal in the Software without restriction, including without limitation
7a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * and/or sell copies of the Software, and to permit persons to whom the
9a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * Software is furnished to do so, subject to the following conditions:
10a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick *
11a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * The above copyright notice and this permission notice (including the next
12a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * paragraph) shall be included in all copies or substantial portions of the
13a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * Software.
14a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick *
15a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * DEALINGS IN THE SOFTWARE.
22a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */
23ac95f2f8c88d39aaa878f61172d9748af13e2c80Eric Anholt
24ac95f2f8c88d39aaa878f61172d9748af13e2c80Eric Anholt#include "ir.h"
2531747155ea3a24190277b125bd188ac8689af719Aras Pranckevicius#include "program/symbol_table.h"
26a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick#include "glsl_parser_extras.h"
27a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick#include "ast.h"
28a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick#include "glsl_types.h"
29a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
30768b55a5268572ff9fd03e57e92775882eb0a821Ian Romanickir_rvalue *
31a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick_mesa_ast_field_selection_to_hir(const ast_expression *expr,
320044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanick				 exec_list *instructions,
33a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick				 struct _mesa_glsl_parse_state *state)
34a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
35953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke   void *ctx = state;
36affc1413ac9f1f077a4ba1a1b7135f73d7a71167Kenneth Graunke   ir_rvalue *result = NULL;
37fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunke   ir_rvalue *op;
38a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
3918238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   op = expr->subexpressions[0]->hir(instructions, state);
40a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
41a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* There are two kinds of field selection.  There is the selection of a
42a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * specific field from a structure, and there is the selection of a
43a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * swizzle / mask from a vector.  Which is which is determined entirely
44a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * by the base type of the thing to which the field selection operator is
45a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * being applied.
46a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
47affc1413ac9f1f077a4ba1a1b7135f73d7a71167Kenneth Graunke   YYLTYPE loc = expr->get_location();
48a43817a483a8c4a480ef4e6dfda2cef899300eb0Ian Romanick   if (op->type->is_error()) {
49affc1413ac9f1f077a4ba1a1b7135f73d7a71167Kenneth Graunke      /* silently propagate the error */
50affc1413ac9f1f077a4ba1a1b7135f73d7a71167Kenneth Graunke   } else if (op->type->is_vector()) {
51affc1413ac9f1f077a4ba1a1b7135f73d7a71167Kenneth Graunke      ir_swizzle *swiz = ir_swizzle::create(op,
52affc1413ac9f1f077a4ba1a1b7135f73d7a71167Kenneth Graunke					    expr->primary_expression.identifier,
53affc1413ac9f1f077a4ba1a1b7135f73d7a71167Kenneth Graunke					    op->type->vector_elements);
54affc1413ac9f1f077a4ba1a1b7135f73d7a71167Kenneth Graunke      if (swiz != NULL) {
55affc1413ac9f1f077a4ba1a1b7135f73d7a71167Kenneth Graunke	 result = swiz;
56a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      } else {
57a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 /* FINISHME: Logging of error messages should be moved into
58affc1413ac9f1f077a4ba1a1b7135f73d7a71167Kenneth Graunke	  * FINISHME: ir_swizzle::create.  This allows the generation of more
59a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	  * FINISHME: specific error messages.
60a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	  */
61a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 _mesa_glsl_error(& loc, state, "Invalid swizzle / mask `%s'",
62a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			  expr->primary_expression.identifier);
63a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
64a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   } else if (op->type->base_type == GLSL_TYPE_STRUCT) {
651660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_dereference_record(op,
661660a2954797e056caba319c5d6c70b0d4be22feCarl Worth					      expr->primary_expression.identifier);
676efaeeea4489941f4916fda3041c2bf4e22b482eIan Romanick
686efaeeea4489941f4916fda3041c2bf4e22b482eIan Romanick      if (result->type->is_error()) {
696efaeeea4489941f4916fda3041c2bf4e22b482eIan Romanick	 _mesa_glsl_error(& loc, state, "Cannot access field `%s' of "
706efaeeea4489941f4916fda3041c2bf4e22b482eIan Romanick			  "structure",
716efaeeea4489941f4916fda3041c2bf4e22b482eIan Romanick			  expr->primary_expression.identifier);
726efaeeea4489941f4916fda3041c2bf4e22b482eIan Romanick      }
73094cf8c199930d958d9e1139467eb8579d082df6Kenneth Graunke   } else if (expr->subexpressions[1] != NULL) {
74094cf8c199930d958d9e1139467eb8579d082df6Kenneth Graunke      /* Handle "method calls" in GLSL 1.20 - namely, array.length() */
75094cf8c199930d958d9e1139467eb8579d082df6Kenneth Graunke      if (state->language_version < 120)
76094cf8c199930d958d9e1139467eb8579d082df6Kenneth Graunke	 _mesa_glsl_error(&loc, state, "Methods not supported in GLSL 1.10.");
77094cf8c199930d958d9e1139467eb8579d082df6Kenneth Graunke
78094cf8c199930d958d9e1139467eb8579d082df6Kenneth Graunke      ast_expression *call = expr->subexpressions[1];
79094cf8c199930d958d9e1139467eb8579d082df6Kenneth Graunke      assert(call->oper == ast_function_call);
80094cf8c199930d958d9e1139467eb8579d082df6Kenneth Graunke
81094cf8c199930d958d9e1139467eb8579d082df6Kenneth Graunke      const char *method;
82094cf8c199930d958d9e1139467eb8579d082df6Kenneth Graunke      method = call->subexpressions[0]->primary_expression.identifier;
83094cf8c199930d958d9e1139467eb8579d082df6Kenneth Graunke
84094cf8c199930d958d9e1139467eb8579d082df6Kenneth Graunke      if (op->type->is_array() && strcmp(method, "length") == 0) {
85094cf8c199930d958d9e1139467eb8579d082df6Kenneth Graunke	 if (!call->expressions.is_empty())
86094cf8c199930d958d9e1139467eb8579d082df6Kenneth Graunke	    _mesa_glsl_error(&loc, state, "length method takes no arguments.");
87094cf8c199930d958d9e1139467eb8579d082df6Kenneth Graunke
88094cf8c199930d958d9e1139467eb8579d082df6Kenneth Graunke	 if (op->type->array_size() == 0)
89094cf8c199930d958d9e1139467eb8579d082df6Kenneth Graunke	    _mesa_glsl_error(&loc, state, "length called on unsized array.");
90094cf8c199930d958d9e1139467eb8579d082df6Kenneth Graunke
91094cf8c199930d958d9e1139467eb8579d082df6Kenneth Graunke	 result = new(ctx) ir_constant(op->type->array_size());
92094cf8c199930d958d9e1139467eb8579d082df6Kenneth Graunke      } else {
93094cf8c199930d958d9e1139467eb8579d082df6Kenneth Graunke	 _mesa_glsl_error(&loc, state, "Unknown method: `%s'.", method);
94094cf8c199930d958d9e1139467eb8579d082df6Kenneth Graunke      }
95a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   } else {
96a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      _mesa_glsl_error(& loc, state, "Cannot access field `%s' of "
97a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick		       "non-structure / non-vector.",
98a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick		       expr->primary_expression.identifier);
99a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
100a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
101807e967c615dc80a264af5a89af7649f95481744Kenneth Graunke   return result ? result : ir_rvalue::error_value(ctx);
102a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
103