glsl: Fix handling of function calls inside nested loops.
[mesa.git] / src / glsl / hir_field_selection.cpp
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 "program/symbol_table.h"
26 #include "glsl_parser_extras.h"
27 #include "ast.h"
28 #include "glsl_types.h"
29
30 ir_rvalue *
31 _mesa_ast_field_selection_to_hir(const ast_expression *expr,
32 exec_list *instructions,
33 struct _mesa_glsl_parse_state *state)
34 {
35 void *ctx = state;
36 ir_rvalue *result = NULL;
37 ir_rvalue *op;
38
39 op = expr->subexpressions[0]->hir(instructions, state);
40
41 /* There are two kinds of field selection. There is the selection of a
42 * specific field from a structure, and there is the selection of a
43 * swizzle / mask from a vector. Which is which is determined entirely
44 * by the base type of the thing to which the field selection operator is
45 * being applied.
46 */
47 YYLTYPE loc = expr->get_location();
48 if (op->type->is_error()) {
49 /* silently propagate the error */
50 } else if (op->type->base_type == GLSL_TYPE_STRUCT
51 || op->type->base_type == GLSL_TYPE_INTERFACE) {
52 result = new(ctx) ir_dereference_record(op,
53 expr->primary_expression.identifier);
54
55 if (result->type->is_error()) {
56 _mesa_glsl_error(& loc, state, "cannot access field `%s' of "
57 "structure",
58 expr->primary_expression.identifier);
59 }
60 } else if (expr->subexpressions[1] != NULL) {
61 /* Handle "method calls" in GLSL 1.20 - namely, array.length() */
62 state->check_version(120, 300, &loc, "methods not supported");
63
64 ast_expression *call = expr->subexpressions[1];
65 assert(call->oper == ast_function_call);
66
67 const char *method;
68 method = call->subexpressions[0]->primary_expression.identifier;
69
70 if (strcmp(method, "length") == 0) {
71 if (!call->expressions.is_empty())
72 _mesa_glsl_error(&loc, state, "length method takes no arguments");
73
74 if (op->type->is_array()) {
75 if (op->type->is_unsized_array())
76 _mesa_glsl_error(&loc, state, "length called on unsized array");
77
78 result = new(ctx) ir_constant(op->type->array_size());
79 } else if (op->type->is_vector()) {
80 if (state->ARB_shading_language_420pack_enable) {
81 /* .length() returns int. */
82 result = new(ctx) ir_constant((int) op->type->vector_elements);
83 } else {
84 _mesa_glsl_error(&loc, state, "length method on matrix only available"
85 "with ARB_shading_language_420pack");
86 }
87 } else if (op->type->is_matrix()) {
88 if (state->ARB_shading_language_420pack_enable) {
89 /* .length() returns int. */
90 result = new(ctx) ir_constant((int) op->type->matrix_columns);
91 } else {
92 _mesa_glsl_error(&loc, state, "length method on matrix only available"
93 "with ARB_shading_language_420pack");
94 }
95 }
96 } else {
97 _mesa_glsl_error(&loc, state, "unknown method: `%s'", method);
98 }
99 } else if (op->type->is_vector() ||
100 (state->ARB_shading_language_420pack_enable &&
101 op->type->is_scalar())) {
102 ir_swizzle *swiz = ir_swizzle::create(op,
103 expr->primary_expression.identifier,
104 op->type->vector_elements);
105 if (swiz != NULL) {
106 result = swiz;
107 } else {
108 /* FINISHME: Logging of error messages should be moved into
109 * FINISHME: ir_swizzle::create. This allows the generation of more
110 * FINISHME: specific error messages.
111 */
112 _mesa_glsl_error(& loc, state, "invalid swizzle / mask `%s'",
113 expr->primary_expression.identifier);
114 }
115 } else {
116 _mesa_glsl_error(& loc, state, "cannot access field `%s' of "
117 "non-structure / non-vector",
118 expr->primary_expression.identifier);
119 }
120
121 return result ? result : ir_rvalue::error_value(ctx);
122 }