2 * Copyright © 2010 Intel Corporation
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:
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
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.
25 #include "glsl_parser_extras.h"
27 #include "glsl_types.h"
30 _mesa_ast_field_selection_to_hir(const ast_expression
*expr
,
31 exec_list
*instructions
,
32 struct _mesa_glsl_parse_state
*state
)
35 ir_rvalue
*result
= NULL
;
38 op
= expr
->subexpressions
[0]->hir(instructions
, state
);
40 /* There are two kinds of field selection. There is the selection of a
41 * specific field from a structure, and there is the selection of a
42 * swizzle / mask from a vector. Which is which is determined entirely
43 * by the base type of the thing to which the field selection operator is
46 YYLTYPE loc
= expr
->get_location();
47 if (op
->type
->is_error()) {
48 /* silently propagate the error */
49 } else if (op
->type
->base_type
== GLSL_TYPE_STRUCT
50 || op
->type
->base_type
== GLSL_TYPE_INTERFACE
) {
51 result
= new(ctx
) ir_dereference_record(op
,
52 expr
->primary_expression
.identifier
);
54 if (result
->type
->is_error()) {
55 _mesa_glsl_error(& loc
, state
, "cannot access field `%s' of "
57 expr
->primary_expression
.identifier
);
59 } else if (expr
->subexpressions
[1] != NULL
) {
60 /* Handle "method calls" in GLSL 1.20 - namely, array.length() */
61 state
->check_version(120, 300, &loc
, "methods not supported");
63 ast_expression
*call
= expr
->subexpressions
[1];
64 assert(call
->oper
== ast_function_call
);
67 method
= call
->subexpressions
[0]->primary_expression
.identifier
;
69 if (strcmp(method
, "length") == 0) {
70 if (!call
->expressions
.is_empty())
71 _mesa_glsl_error(&loc
, state
, "length method takes no arguments");
73 if (op
->type
->is_array()) {
74 if (op
->type
->is_unsized_array())
75 _mesa_glsl_error(&loc
, state
, "length called on unsized array");
77 result
= new(ctx
) ir_constant(op
->type
->array_size());
78 } else if (op
->type
->is_vector()) {
79 if (state
->ARB_shading_language_420pack_enable
) {
80 /* .length() returns int. */
81 result
= new(ctx
) ir_constant((int) op
->type
->vector_elements
);
83 _mesa_glsl_error(&loc
, state
, "length method on matrix only available"
84 "with ARB_shading_language_420pack");
86 } else if (op
->type
->is_matrix()) {
87 if (state
->ARB_shading_language_420pack_enable
) {
88 /* .length() returns int. */
89 result
= new(ctx
) ir_constant((int) op
->type
->matrix_columns
);
91 _mesa_glsl_error(&loc
, state
, "length method on matrix only available"
92 "with ARB_shading_language_420pack");
96 _mesa_glsl_error(&loc
, state
, "unknown method: `%s'", method
);
98 } else if (op
->type
->is_vector() ||
99 (state
->ARB_shading_language_420pack_enable
&&
100 op
->type
->is_scalar())) {
101 ir_swizzle
*swiz
= ir_swizzle::create(op
,
102 expr
->primary_expression
.identifier
,
103 op
->type
->vector_elements
);
107 /* FINISHME: Logging of error messages should be moved into
108 * FINISHME: ir_swizzle::create. This allows the generation of more
109 * FINISHME: specific error messages.
111 _mesa_glsl_error(& loc
, state
, "invalid swizzle / mask `%s'",
112 expr
->primary_expression
.identifier
);
115 _mesa_glsl_error(& loc
, state
, "cannot access field `%s' of "
116 "non-structure / non-vector",
117 expr
->primary_expression
.identifier
);
120 return result
? result
: ir_rvalue::error_value(ctx
);