hir_field_selection.cpp revision 31747155ea3a24190277b125bd188ac8689af719
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#include "ir.h"
25#include "main/imports.h"
26#include "program/symbol_table.h"
27#include "glsl_parser_extras.h"
28#include "ast.h"
29#include "glsl_types.h"
30
31struct ir_rvalue *
32_mesa_ast_field_selection_to_hir(const ast_expression *expr,
33				 exec_list *instructions,
34				 struct _mesa_glsl_parse_state *state)
35{
36   void *ctx = state;
37   ir_rvalue *result = NULL;
38   ir_rvalue *op;
39
40   op = expr->subexpressions[0]->hir(instructions, state);
41
42   /* There are two kinds of field selection.  There is the selection of a
43    * specific field from a structure, and there is the selection of a
44    * swizzle / mask from a vector.  Which is which is determined entirely
45    * by the base type of the thing to which the field selection operator is
46    * being applied.
47    */
48   YYLTYPE loc = expr->get_location();
49   if (op->type->is_error()) {
50      /* silently propagate the error */
51   } else if (op->type->is_vector()) {
52      ir_swizzle *swiz = ir_swizzle::create(op,
53					    expr->primary_expression.identifier,
54					    op->type->vector_elements);
55      if (swiz != NULL) {
56	 result = swiz;
57      } else {
58	 /* FINISHME: Logging of error messages should be moved into
59	  * FINISHME: ir_swizzle::create.  This allows the generation of more
60	  * FINISHME: specific error messages.
61	  */
62	 _mesa_glsl_error(& loc, state, "Invalid swizzle / mask `%s'",
63			  expr->primary_expression.identifier);
64      }
65   } else if (op->type->base_type == GLSL_TYPE_STRUCT) {
66      result = new(ctx) ir_dereference_record(op,
67					      expr->primary_expression.identifier);
68
69      if (result->type->is_error()) {
70	 _mesa_glsl_error(& loc, state, "Cannot access field `%s' of "
71			  "structure",
72			  expr->primary_expression.identifier);
73      }
74   } else if (expr->subexpressions[1] != NULL) {
75      /* Handle "method calls" in GLSL 1.20 - namely, array.length() */
76      if (state->language_version < 120)
77	 _mesa_glsl_error(&loc, state, "Methods not supported in GLSL 1.10.");
78
79      ast_expression *call = expr->subexpressions[1];
80      assert(call->oper == ast_function_call);
81
82      const char *method;
83      method = call->subexpressions[0]->primary_expression.identifier;
84
85      if (op->type->is_array() && strcmp(method, "length") == 0) {
86	 if (!call->expressions.is_empty())
87	    _mesa_glsl_error(&loc, state, "length method takes no arguments.");
88
89	 if (op->type->array_size() == 0)
90	    _mesa_glsl_error(&loc, state, "length called on unsized array.");
91
92	 result = new(ctx) ir_constant(op->type->array_size());
93      } else {
94	 _mesa_glsl_error(&loc, state, "Unknown method: `%s'.", method);
95      }
96   } else {
97      _mesa_glsl_error(& loc, state, "Cannot access field `%s' of "
98		       "non-structure / non-vector.",
99		       expr->primary_expression.identifier);
100   }
101
102   return result ? result : ir_call::get_error_instruction(ctx);
103}
104